4

I have searched a lot but i could't find any topic about this. My question is: Is struct variable a pointer? And if every input are stored in struct members' addresses, so what are struct variable used for?

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.

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

6487600

6487616

6487620

8
  • 1
    Read up on the & operator. Read up on printf() & Cos format specifier. Commented Jun 21, 2018 at 15:30
  • @Yunnosch I changed %d to %p but it still printed out that f hold a address and f's address is the same as f.a's address. Commented Jun 21, 2018 at 15:36
  • And reread on structs. Commented Jun 21, 2018 at 15:39
  • @ThuanNguyen your comment is answered in the last part of my answer. :) Commented Jun 21, 2018 at 15:42
  • Aside: in general, put the newline at the other end as in printf("%p\n", (void*)&f); because it is more idiomatic, and to ensure the line is sent to output. Commented Jun 21, 2018 at 15:44

3 Answers 3

5

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.

To elaborate, to print a pointer, you need to use %p conversion specifier, with a cast to void * for the argument.

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. [...]

Sign up to request clarification or add additional context in comments.

Comments

4

Is struct variable a pointer?

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 ints):

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

Members are laid out in the order declared - f.a occupies the first four bytes of the object, f.b occupies the next four bytes, and f.c occupies the last byte. Additional "padding" bytes may be present to satisfy alignment requirements (in this case, the compiler will likely add 3 padding bytes after f.c so that the next object starts on an address that's a multiple of 4).

As you can see, the address of the first member of the struct (f.a) will be the same as the address of the entire struct object. 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 f.c, so sizeof f will likely be larger than sizeof f.a + sizeof f.b + sizeof f.c.

4 Comments

So it's like when i delare int some_variable , the complier allocate four bytes memory for this some_variable . This declaration of struct's member: int a and struct's variable: f means that: i have a int f.a occupies 4 bytes. So what is the usage of f, because i can use directly a, b and c.
Sorry for asking another question, but what is the memory allocation for this structure if i have more variables like f1,f2,etc.
@ThuanNguyen: struct types are used to represent objects that can be described by multiple attributes - for example, you can have a struct person type that contains a name, birthdate, sex, and other identifying information. Instead of tracking each attribute separately, you gather them into a single object. It makes handling complex information more convenient. As for memory allocation, each additional variable is laid out like I described in the answer. If f takes 9 bytes, so does f1, f2, etc.
Thank you very much,i understand it now.
0

The & operator as you are using it is the address-of operator. It will return the address of any variable, heap allocated or not.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.