简体   繁体   English

C,结构的第一位成员

[英]C, first member of struct

I have yet another newbie C question: Why does the first member of a struct return an adress not similar to the structs own pointer-adress when not initialized? 我还有另一个新手C问题:为什么在未初始化的情况下,结构的第一个成员返回的地址与结构自己的指针地址不同?

Example: 例:

struct Metadata {
    int message_ID;
    //other members...
    //...
};

struct Metadata* baseMetadataPtr = (struct Metadata*) malloc(sizeof(struct Metadata)*100);

printf("baseMetadataPtr: %d\n", baseMetadataPtr);
//consoll says "baseMetadataPtr: 2636496"

printf("baseMetadataPtr->message_ID: %d\n", baseMetadataPtr->message_ID);
//consoll says "baseMetadataPtr->message_ID: 2621636"

Your second printf call is wrong. 您的第二个printf呼叫是错误的。 It should be: 它应该是:

printf("baseMetadataPtr->message_ID: %p\n", &baseMetadataPtr->message_ID);
//         need to use %p for pointer ^     ^ need unary-& operator

As written right now, the integer value of message_ID is being printed. 如现在所写,正在打印message_ID的整数值。 You need to take the address of baseMetadataPtr->message_ID . 您需要获取baseMetadataPtr->message_ID的地址。 Also note that if you want to print a pointer, you should use the %p format specifier, not %d (which prints an integer). 还要注意,如果要打印指针,则应使用%p格式说明符,而不要使用%d (打印整数)。

The address of the first data member of a struct-type object will always be the same as the address of the struct-type object itself. 结构类型对象的第一个数据成员的地址将始终与结构类型对象本身的地址相同。 This is guaranteed because no padding is permitted at the beginning of a struct (padding is permitted between data elements or at the end of the struct, though). 这是可以保证的,因为在结构的开头不允许填充(尽管在数据元素之间或在结构的结尾允许填充)。

I like drawings. 我喜欢图纸。 They help me "see" stuff 他们帮助我“看”东西

struct Metadata *baseMetadataPtr;
baseMetadataPtr = malloc(100 * sizeof *baseMetadataPtr);

With that, and assuming a linear memory (the boxes below), and ignoring the space requirements for the different objects, we have 这样,假设有线性存储器(下面的方框),并且忽略了不同对象的空间要求,

|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|...|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|...
   ^^^^^^^ baseMetadataPtr
  (of type (struct Metadata *))

   ******* ===========================>
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|...|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|...
   ^^^^^^^ baseMetadataPtr             ^^^^^^ ^^^^^^^ ^^^^^^^ ^^^^^ ^^ ...
  (of type (struct Metadata *))        *baseMetadataPtr (struct Metadata)
                                               *(baseMetadataPtr+1)
                                                      baseMetadataPtr[2]

And, zooming in on the part on the left 并且,放大左侧的部分

*baseMetadataPtr
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|...
   ^^^ Message_ID (type (int))
       ^^^^^^^ Message_Len (type (size_t))
               ^^^^ ... other members, followed by another object of type (struct Metadata)

This prints the memory address of the pointer: 这将打印指针的内存地址:

printf("baseMetadataPtr: %d\n", baseMetadataPtr); 

and this prints the memory address of the variable inside the struct: 并在结构内打印变量的内存地址:

printf("baseMetadataPtr->message_ID: %d\n", &baseMetadataPtr->message_ID); 

And that's why they are not neighbors. 这就是为什么他们不是邻居。

baseMetadataPtr->message_ID prints the value of the variable message_ID . baseMetadataPtr->message_ID打印变量message_ID Since you have not initialized it, it is containing garbage. 由于尚未初始化,因此其中包含垃圾。 To print its address you need to do &baseMetadataPtr->message_ID . 要打印其地址,您需要执行&baseMetadataPtr->message_ID Also, you need to use the format specifier %p to print pointer values. 另外,您需要使用格式说明符%p来打印指针值。

The first printf() prints the current value of the pointer, IE, what address it is pointing. 第一个printf()打印指针IE的当前值以及它指向的地址。

The second printf prints the value of the Metadata member message_ID. 第二个printf打印元数据成员message_ID的值。

To get what you want, try: printf("baseMetadataPtr->message_ID: %d\\n", &baseMetadataPtr->message_ID); 要获得所需的内容,请尝试:printf(“ baseMetadataPtr-> message_ID:%d \\ n”,&baseMetadataPtr-> message_ID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM