简体   繁体   English

C - struct变量是指针吗?

[英]C - Is struct variable a pointer?

I have searched a lot but i could't find any topic about this. 我搜索了很多,但我找不到任何关于此的话题。 My question is: Is struct variable a pointer? 我的问题是:struct变量是指针吗? And if every input are stored in struct members' addresses, so what are struct variable used for? 如果每个输入都存储在struct members的地址中,那么struct变量用于什么?

This is the code i used for checking struct variable, when i printed what is stored in the variable, it gave me another address but i couldn't figure what that address is used for. 这是我用来检查struct变量的代码,当我打印存储在变量中的内容时,它给了我另一个地址,但我无法确定该地址的用途。

Here is my code and its output: 这是我的代码及其输出:

 struct test
 {
 int a;
 int b;
 char c;
 }; 

 int main()
 {

 struct test f;

 printf("%u", &f);
 printf("\n%d", f);
 printf("\n%u", &f.a);
 printf("\n%u", &f.b);
 }

Output: 输出:

6487616 6487616

6487600 6487600

6487616 6487616

6487620 6487620

Short answer: No . 简答:

Long version: All your print statements in the aforesaid program invoke undefined behaviour, as the supplied argument does not match the conversion specifier. 长版本:上述程序中的所有print语句都会调用未定义的行为,因为提供的参数与转换说明符不匹配。

To elaborate, to print a pointer, you need to use %p conversion specifier, with a cast to void * for the argument. 要详细说明,要打印指针,您需要使用%p转换说明符,并为参数使用%p转换为void *

There is no generic ( one-for-all sort ) format specifier for a structure variable. 结构变量没有通用一对一排序 )格式说明符。 You need to choose individual members (or member of member, thereof) which has a corresponding format specifier, and pass that as the argument. 您需要选择具有相应格式说明符的单个成员(或其成员),并将其作为参数传递。

That said, regarding the first member and the access, 也就是说,关于第一个成员和访问,

[...] A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. [...]指向结构对象的指针(适当转换)指向其初始成员(或者如果该成员是位字段,则指向它所在的单位),反之亦然。 [...] [...]

Is struct variable a pointer? struct变量是指针吗?

No. Given the struct definition: 不。给出结构定义:

struct test
{
  int a;
  int b;
  char c;
}; 

and the declaration: 和声明:

struct test f;

you get the following in memory (assuming 4-byte int s): 你在内存中得到以下内容(假设4字节int ):

   +---+ ---+
f: |   |    |
   +---+    |
   |   |    |
   +---+    +--- f.a
   |   |    |
   +---+    |
   |   |    |
   +---+ ---+
   |   |    |
   +---+    |
   |   |    |
   +---+    +--- f.b
   |   |    |
   +---+    |
   |   |    |
   +---+ ---+
   |   | f.c
   +---+

Members are laid out in the order declared - fa occupies the first four bytes of the object, fb occupies the next four bytes, and fc occupies the last byte. 成员按照声明的顺序排列 - fa占用对象的前四个字节, fb占用接下来的四个字节, fc占据最后一个字节。 Additional "padding" bytes may be present to satisfy alignment requirements (in this case, the compiler will likely add 3 padding bytes after fc so that the next object starts on an address that's a multiple of 4). 可能存在额外的“填充”字节以满足对齐要求(在这种情况下,编译器可能在fc之后添加3个填充字节,以便下一个对象在4的倍数的地址上开始)。

As you can see, the address of the first member of the struct ( fa ) will be the same as the address of the entire struct object. 如您所见,struct( fa )的第一个成员的地址将与整个struct对象的地址相同。 There's no sort of padding or metadata before the first member. 在第一个成员之前没有填充或元数据。

To look at the various addresses and sizes, use the following: 要查看各种地址和大小,请使用以下命令:

printf( "address of f: %p\n", (void *) &f );
printf( "size of f: %zu\n", sizeof f );

printf( "address of f.a: %p\n", (void *) &f.a );
printf( "size of f.a: %zu\n", sizeof f.a );

printf( "address of f.b: %p\n", (void *) &f.b );
printf( "size of f.b: %zu\n", sizeof f.b );

printf( "address of f.c: %p\n", (void *) &f.c );
printf( "size of f.c: %zu\n", sizeof f.c );

Like I said above, the compiler will most likely add padding bytes after fc , so sizeof f will likely be larger than sizeof fa + sizeof fb + sizeof fc . 就像我上面说的那样,编译器很可能在fc之后添加填充字节,因此sizeof f可能会大于sizeof f sizeof fa + sizeof fb + sizeof fc

The & operator as you are using it is the address-of operator . 您正在使用的&运算符是地址运算符 It will return the address of any variable, heap allocated or not. 它将返回任何变量的地址,堆分配与否。

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

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