Last active
February 22, 2021 06:00
-
-
Save honux77/442a0f8c6562526e2dadd45e94831421 to your computer and use it in GitHub Desktop.
21-02-22-codesquad-lecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
double d[] = {1.0, 3.0, 5.0}; //전역변수, data 영역에 저장됨 | |
int main(int argc, char const *argv[]) | |
{ | |
int *a = malloc(sizeof(int) * 10); //동적 할당 힙에 저장됨 | |
a[0] = 1; | |
a[1] = 2; | |
a[2] = 3; | |
int b[] = {2, 4, 6}; //지역변수, 스택에 배열 저장 | |
//배열의 기본 성질 | |
printf("%d %d\n", a[0], a[1]); | |
printf("address of a: %p\n", a); | |
printf("a[0]: %d\n", *(a + 0)); | |
printf("a[0]: %d\n", *(0 + a)); | |
printf("value *a: %d\n", a[0]); //a[i] = *(a + i) | |
printf("value *a: %d\n", 0[a]); //a[i] = i[a] | |
printf("value *(a + 2): %d\n", a[2]); //*(a + 2) | |
printf("value *(a + 2): %d\n", *(a + 2)); //a[2] | |
//포인터와 배열 | |
int *b = a; | |
printf("a == b ? %d\n", a == b); //true | |
printf("address b: %p\n", b); | |
printf("address b + 1: %p\n", b + 1); | |
printf("value *(b + 1): %d\n", *(b + 1)); //b[1] | |
//double형 포인터 주소 계산 | |
double *p = d; | |
printf("address p %p\n", p); | |
printf("address p + 1 %p \n", p + 1); | |
printf("value *(p + 1) %f \n", *(p + 1)); | |
free(a); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//void (*fn)(int) 매개변수 int 리턴타입 void인 함수 포인터 | |
void each(int *a, int size, void (*fn)(int)) { | |
for(int i = 0; i < size; i++) { | |
fn(a[i]); | |
} | |
} | |
void func(int x) { | |
printf("x: %d\n", x); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
int a[] = {1, 2, 3}; | |
each(a, 3, func); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment