복잡한 C 선언
인터넷에서 몇 가지 코드를 살펴보고 다음을 발견했습니다.
float * (*(*foo())[SIZE][SIZE])()
이 선언문을 어떻게 읽습니까? 이러한 복잡한 선언을 읽기위한 특정 규칙이 있습니까?
나는 이것을 한동안하지 않았다!
시작해서 foo
오른쪽으로 가십시오.
float * (*(*
foo()
)[SIZE][SIZE])()
foo는 인수가없는 함수입니다 ...
닫는 괄호가 있기 때문에 바로 갈 수 없습니다. 왼쪽으로 이동 :
float * (*(
* foo()
)[SIZE][SIZE])()
foo는 포인터를 반환하는 인수가없는 함수입니다.
더 왼쪽으로 갈 수 없으니 괄호를 넘고 다시 오른쪽으로 가자
float * (*
(* foo())
[SIZE][SIZE])()
float * (*
(* foo())[SIZE]
[SIZE])()
float * (*
(* foo())[SIZE][SIZE]
)()
foo는 SIZE 배열의 배열에 대한 포인터를 반환하는 인수가없는 함수입니다.
닫는 괄호에 도달했습니다. 포인터 기호에 도달하려면 다시 왼쪽으로 이동하십시오.
float * (
*(* foo())[SIZE][SIZE]
)()
foo는 SIZE 포인터의 SIZE 배열 배열에 대한 포인터를 반환하는 인수가없는 함수입니다.
다시 왼쪽 괄호를 사용하여 교차하고 다시 오른쪽으로 이동합니다.
float *
( *(* foo())[SIZE][SIZE])
()
float *
( *(* foo())[SIZE][SIZE])()
foo는 인수가없는 함수에 대한 SIZE 포인터의 SIZE 배열 배열에 대한 포인터를 반환하는 인수가없는 함수입니다 ...
그리고 끝까지 왼쪽
float * ( *(* foo())[SIZE][SIZE])()
foo는 인수가없는 함수로 SIZE 배열의 배열에 대한 포인터를 반환합니다. SIZE 배열의 포인터는 인수가없는 함수에 대한 포인터를 반환합니다.
그리고 그것을 쓴 사람은 그에게 사용하도록 가르쳐주세요 typedef
.
// Function that returns a pointer to float
typedef float* PFloatFunc ();
// Array of pointers to PFloatFunc functions
typedef PFloatFunc* PFloatFuncArray2D[SIZE][SIZE];
// Function that returns a pointer to a PFloatFuncArray2D
PFloatFuncArray2D* foo();
표준 규칙 : 가장 왼쪽 식별자를 찾아 기억, 당신의 방법을 작동 할 수 []
와 ()
결합하기 전에 *
:
foo -- foo
foo() -- is a function
*foo() -- returning a pointer
(*foo())[SIZE] -- to a SIZE-element array
(*foo())[SIZE][SIZE] -- of SIZE-element arrays
*(*foo())[SIZE][SIZE] -- of pointers
(*(*foo())[SIZE][SIZE])() -- to functions
* (*(*foo())[SIZE][SIZE])() -- returning pointers
float * (*(*foo())[SIZE][SIZE])(); -- to float
따라서 포인터를 반환하는 많은 함수가 있다고 상상해보십시오 float
.
float *quux();
float *bar();
float *bletch();
float *blurga();
2x2 테이블에 저장한다고 가정 해 보겠습니다.
float *(*tab[SIZE][SIZE])() = {quux, bar, bletch, blurga};
tab
에 대한 포인터를 반환하는 함수에 대한 포인터의 SIZE x SIZE 배열입니다 float
.
이제 함수가 해당 테이블에 대한 포인터를 반환하기를 원합니다.
float *(*(*foo())[SIZE][SIZE])()
{
static float *(*tab[SIZE][SIZE])() = {quux, bar, bletch, blurga};
return &tab;
}
다른 함수의 테이블을 작성하거나 동일한 함수를 다르게 구성하는 여러 함수를 가질 수 있습니다.
float *(*(*qwerbl())[SIZE][SIZE])()
{
static float *(*tab[SIZE][SIZE])() = {blurga, bletch, bar, quux};
return tab;
}
이것이 제가 이런 일을 할 수있는 유일한 이유입니다. 이러한 유형은 야생에서 자주 볼 수 없습니다 (비록 가끔 발생하지만 비슷한 유형의 가증 한 글을 쓴 죄책감이 있습니다).
cdecl.org 에 따르면
배열에 대한 포인터를 반환하는 함수로 foo 선언 배열의 크기
손으로 해독하려면 Luchian Grigore가 제공 한 나선형 규칙을 사용하십시오.
여기서 가장 좋은 방법은 일련의 typedef로 변환하는 것입니다.
typedef float * fnReturningPointerToFloat();
typedef fnReturningPointerToFloat* fnArray[SIZE][SIZE];
fnArray* foo();
일반적으로 cdecl.org를 시도해 볼 수 있지만SIZE
SIZE
12로 바꾸면 다음을 얻을 수 있습니다.
declare foo as function returning pointer to array 12 of array 12 of pointer to function returning pointer to float
I'm not sure that really helps you!
Two observations here:
- I'm guessing that this code didn't have a comment beside it explaining what the purpose of it was (i.e. not the technical explanation of what it is but what it is achieving from a functional / business perspective) If a programmer needs to use something as complex as this, they should be good enough to explain to future maintainers what purpose it serves.
- Certainly in C++ there are more obvious and probably safer ways of achieving the same thing.
This document gaves me the best clue about how to easily ready any C declaration :
http://c-faq.com/decl/spiral.anderson.html
There are three simple steps to follow:
Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements:
[X]
or[]
=> Array X size of ... or Array undefined size of ...
(type1, type2)
=> function passing type1 and type2 returning ...
*
=> pointer(s) to ...Keep doing this in a spiral/clockwise direction until all tokens have been covered.
Always resolve anything in parenthesis first!
Example :
+-------+
| +-+ |
| ^ | |
char *str[10];
^ ^ | |
| +---+ |
+-----------+
Question we ask ourselves: What is str?
``str is an...
- We move in a spiral clockwise direction starting with `str' and the first character we see is a `[' so, that means we have an array, so...
``str is an array 10 of...
- Continue in a spiral clockwise direction, and the next thing we encounter is the `*' so, that means we have pointers, so...
``str is an array 10 of pointers to...
- Continue in a spiral direction and we see the end of the line (the `;'), so keep going and we get to the type `char', so...
``str is an array 10 of pointers to char''
We have now ``visited'' every token; therefore we are done!
from http://cdecl.org/
declare foo as function returning pointer to array SIZE of array SIZE of pointer to function returning pointer to float
참고URL : https://stackoverflow.com/questions/15111526/complex-c-declaration
'IT TIP' 카테고리의 다른 글
문화에 관계없이 소수점 이하 자릿수 찾기 (0) | 2020.11.02 |
---|---|
버튼 그룹의 너비를 100 %로 설정하고 버튼의 너비를 같게 만드시겠습니까? (0) | 2020.11.02 |
Emacs를 사용하여 현재 날짜와 시간을 파일에 어떻게 삽입 할 수 있습니까? (0) | 2020.11.02 |
UIScrollView 콘텐츠의 보이는 사각형 가져 오기 (0) | 2020.11.02 |
프로젝트로드 오류 : 3 개의 패싯 세부 사항을로드 할 수 없습니다. (0) | 2020.11.02 |