开发者

Dealing with Structs in C

Suppose struct_name is the name of a struct I've defined, and array is a member in the开发者_如何学JAVA struct defined as char array[o]

what does the following line produce? (*struct_name).array an address location?


yes (assuming struct_name is a pointer to your struct, otherwise the dereferencing just doesn't make sense)

btw, why not do struct_name->array ?


If you've defined struct_name as an instance of your struct like this:

struct your_struct struct_name;

You want struct_name.array which, yes, produces an address to the array member. If you've defined struct_name as an instance of your struct like this:

struct your_struct *struct_name;
struct_name = malloc(sizeof(struct your_struct));

You want struct_name->array, which also returns the address of array.

If you've defined struct_name as the name of the struct itself like this:

typedef struct _struct_name {
    char array[5];
} struct_name;

Then you don't know what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜