2008-10-13 3 views

답변

15

당신은 할 수 있습니다

// In global scope 
int x, y; 
const struct {int *px, *py; } s = {&x, &y}; 
5
const struct mytype foo = {&var1, &var2}; 
1

const를 구조체는 정적 초기화 할 수 있습니다.

0

그러나이 다음과 같은 몇 가지 struct 경우 :

struct Foo 
{ 
    const int a; 
    int b; 
}; 

을 우리가 동적으로 malloc를 사용하여 struct에 대한 포인터를 만들려면, 그래서 우리는 트릭을 재생할 수 있습니다 :

struct Foo foo = { 10, 20 }; 
char *ptr = (char*)malloc(sizeof(struct Foo)); 
memcpy(ptr, &foo, sizeof(foo)); 
struct Foo *pfoo = (struct Foo*)ptr; 

이있다 어떤 함수가 포인터를 반환해야 할 때 매우 유용합니다. struct Foo

관련 문제