2008-10-13 5 views

cevap

15

Sen edebilirsiniz:

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

Bir const struct sadece durağan olarak hazırlanabilirler.

0

Ama aşağıdaki gibi bazı struct ise:

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

ve dinamik olarak malloc kullanılarak struct işaretçiyi oluşturmak istiyoruz, bu yüzden hile oynayabilir:

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

şudur Bazı işlevler özellikle işaretçiyi struct Foo

'a döndürdüğünde çok kullanışlıdır