Nice programing

C에서 "[*]"(별표 수정 자)는 무엇을 의미합니까?

nicepro 2020. 10. 30. 20:59
반응형

C에서 "[*]"(별표 수정 자)는 무엇을 의미합니까?


C11 파서 (교육용)를 구현하는 동안 C11 (470 페이지) 뿐만 아니라 C99 (412 페이지) (Johannes에게 감사합니다!)에서도 직접 선언자가 다음과 같이 정의 된다는 사실을 발견했습니다 .

(6.7.6) direct-declarator:  
    direct-declarator [ type-qualifier-list? * ]

처음에는 이것이 문법 오류라고 생각했습니다 (유형 목록은 선택 사항이 아니어야 함). 그러나 참조 컴파일러 (clang)에서 이것을 시도했을 때 예상치 못한 오류가 발생했습니다.

int array[*] = { 1, 2, 3 };
// error: star modifier used outside of function prototype

그래서 분명히 (clang에서) 이것은 star modifier 라고합니다 .

나는 그것들이 함수 시그니처에서만 사용될 수 있다는 것을 금방 배웠습니다.

void foobar(int array[*])

그러나 선언에서만 사용할 수 있습니다. 함수 정의에서 사용하려고하면 오류가 발생합니다.

void foobar(int array[*]) {
    // variable length array must be bound in function definition
}

내가 말할 수있는 한, 의도 된 동작은 [*]함수 선언에서 사용한 다음 함수 정의에서 고정 된 숫자를 사용하는 것입니다.

// public header
void foobar(int array[*]);

// private implementation
void foobar(int array[5]) {

}

그러나 나는 그것을 본 적이 없으며 그것의 목적도 잘 이해하지 못합니다.

  1. 목적은 무엇이며 추가 된 이유는 무엇입니까?
  2. 차이점은 int[]무엇입니까?
  3. 차이점은 int *무엇입니까?

목적은 무엇이며 추가 된 이유는 무엇입니까?

목적은 가변 길이 2 차원 배열이 함수 매개 변수로 사용될 때 볼 수 있습니다. 함수

int foo(int n, int m, int a[n][m])  {...}   

다음 중 하나로 프로토 타입을 만들 수 있습니다.

int foo(int , int, int [][*]);
int foo(int , int, int a[*][*]);
int foo(int , int, int (*a)[*]);
int foo(int n, int, int a[n][*]);
int foo(int , int m, int a[*][m]);
int foo(int , int m, int (*a)[m]);
int foo(int n, int m, int a[n][m]); 

2 차원 배열의 경우 함수 매개 변수로 사용하면 2 차원의 크기를 생략 할 수 없습니다. 함수 프로토 타입에서 첫 번째 변수의 이름이 생략되면 배열의 길이 (두 번째 차원)를 지정할 수 없습니다. *어레이의 길이가 두 번째 매개 변수에 의해 결정된다는 단서를 제공한다.

차이점은 int[]무엇입니까?
차이점은 int *무엇입니까?

1D 배열의 경우 함수 정의용

int bar(int n, int a[n]} {...}  

다음 프로토 타입 중 하나가 유효합니다.

int bar (int , int *);
int bar (int , int [*]);
Int bar (int , int []);
int bar (int n, int a[]);
int bar (int n, int a[n]);
int bar (int n, int [n]);   

이 경우 어느 쪽 *n컴파일러가 모두 취급으로 필요하지 않습니다 int [*]int [n]같이 int *. 따라서 1 차원 배열을 사용하면 큰 차이를 볼 수 없습니다.


NOTE: When using variable length array as a function parameter, order of parameter is important. Order of parameters for first four prototypes of bar can be switched, but in latter two first parameter must not be the array itself.

int bar (int a[n], int n);  //Wrong. Compiler has not yet seen 'n'.

The C rationale document for C99 says

A function prototype can have parameters that have variable length array types (§6.7.5.2) using a special syntax as in

int minimum(int, int [*][*]);

This is consistent with other C prototypes where the name of the parameter need not be specified.


What's the difference with int[]

What's the difference with int *.

I think it's simply that those types in a function prototype means "pointer", while a [*] in a non-top position (int[*] still equals int[] I think, in a function prototype) actually is valid and means array

// not recommended though: it is now unclear what the parameters
// mean to human callers!
void f(int, int [][*]);

void f(int n, int x[][n]) {
    x[1][0] = 1;
}

int main() {
   int a[2][1];
   f(1, a);
   printf("%d\n", a[1][0]);
}

As for the purpose, when indexing the array in the function definition, the compiler needs to know how many integers of the next index to skip when giving the first index (x[i] skips i * n integers in f above). But this information is not needed in the non-defining prototype declaration, hence it can be left out and replaced by *.

참고URL : https://stackoverflow.com/questions/38775392/what-does-star-modifier-mean-in-c

반응형