Home > Net >  What happens when I declare an array in a struct in C language?
What happens when I declare an array in a struct in C language?

Time:12-30

I'm having a hard time understanding what happens when I declare an array in a struct. Talking about normal arrays (int, float, char... type) I know that the name of the array is itself a pointer to the first element of the array. However, let's say I have a struct like this:

struct student
{
   char name[12];
   int age;
}

and in the main function let's say I declare: struct student s1; Well, I don't understand how this variable is allocated in memory. Are there 4 bytes for the age, and 10 bytes for the string, but what about the pointer named s1.name (which is a pointer to the first element of the string, right?), does it get allocated as well in the new struct or not?

CodePudding user response:

Arrays are not pointers, and neither are their "names", whatever that means.

They are implicitly converted to pointers in most scenarios, but the resulting pointer is computed on the fly, and doesn't generally exist in memory before the conversion.

An array (of any element type) is stored in memory as N consecutive elements, and nothing else.

So, char name[12] occupies 12 bytes (which store 12 characters). int age occupies 4 bytes on most modern platforms.

Thus struct Student occupies 16 bytes. An array of struct Student of size N occupies sizeof(struct Student) * N bytes.

CodePudding user response:

I know that the name of the array is itself a pointer

This is an incorrect statement. Arrays are arrays not pointers. But used in expressions with rare exceptions an array designator is implicitly converted to a pointer to its first element.

You declared the type specifier struct student.

struct student
{
   char name[12];
   int age;
};

To determine the size of an object of this type you can write

printf( "sizeof( struct student ) = %zu\n", sizeof( struct student ) );

Here is a demonstration program.

#include <stdio.h>

int main( void )
{
    struct student
    {
        char name[12];
        int age;
    };

    printf( "sizeof( struct student ) = %zu\n", sizeof( struct student ) );

    struct student s;

    printf( "sizeof( s ) = %zu\n", sizeof( s ) );
    printf( "sizeof( s.name ) = %zu\n", sizeof( s.name ) );
    printf( "sizeof( s.age ) = %zu\n", sizeof( s.age ) );
}

The program output might look like

sizeof( struct student ) = 16
sizeof( s ) = 16
sizeof( s.name ) = 12
sizeof( s.age ) = 4

That is an object of the structure type occupies 16 bytes. The first 12 bytes are occupied by its data member (array) name and the last 4 bytes are occupied by the data member age.

CodePudding user response:

to answer your specific question. There is no 'pointer named s.name' there is a variable called s.name. This variable refers to a 12 char array allocated at offset 0 of the struct s. In some circumstances the compiler will treat an array as though it were a pointer to the first element, in other cases not. So

sizeof(s.name) = 12

treating s.name as what it really is, a char array. Whereas a real pointer, say char *p, would produce

sizeof(p) = 4 or 8 depending on 32 or 64 bit

You can see that s.name is treated as a pointer in other places because these both work:

strlen(s.name);
strlen(p);

strlen is declared as taking char*

  • Related