C: (pointer) 배열 변수 와 포인터 의 차이

1. sizeof (배열) = 배열 의 길이;sizeof (배열 을 가리 키 는 포인터) = 포인터 크기 4 또는 8
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <stdio.h>                                                      int main( int argc, const char * argv[]) {                                                               char s[] = "hello world!" ;      char *t = s;                                                               printf ( "sizeof(s) is %li
"
, sizeof (s));      printf ( "sizeof(t) is %li
"
, sizeof (t));                                                               return 0; }
output:
sizeof(s) is 13
sizeof(t) is 8
 
2. char s [] 중의 & s 는 s 와 같 고 char s [] 의 주 소 를 취한 다.
한편, char * t = s 의 & t 는 t 와 같 지 않 고 t 포인터 변수 자체 의 주 소 를 가 져 옵 니 다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include <stdio.h>                                         int main( int argc, const char * argv[]) {                                                  char s[] = "hello world!" ;      char *t = s;                                                  // &s == s; What is the address of the s array?      printf ( "&s is %p
"
, &s);      // &t != t; What is the address of the t variable?      printf ( "&t is %p
"
, &t);                                                  return 0; }
output:
&s is 0x7fff5fbffa2b
&t is 0x7fff5fbffa20
 
3. 포인터, 메모리 가 공간 을 분배 하기 때문에 포인터 가 값 을 다시 할당 할 수 있 습 니 다.배열 변수 와 배열 요소 의 공용 주 소 를 다시 할당 하면 컴 파일 오류 가 발생 합 니 다.
1
2
3
4
5
6
7
8
9
10
11
12 #include <stdio.h>                             int main( int argc, const char * argv[]) {                                      char s[] = "hello world!" ;      char *t = s;                                      s = t; // Error: Array type is not assignable                                      return 0; }
C 프로 그래 밍 iOS 개발

좋은 웹페이지 즐겨찾기