2

I am trying to figure out the sizeof(p) where p is the struct defined below; but, when I try and run the following code:

#include <stdio.h>

struct p
{
    char x;
    int y;
};

int main()
{
    printf("%d", sizeof(p));
    return 0;
}

I receive this error:

main.c: In function ‘main’:
main.c:19:25: error: ‘p’ undeclared (first use in this function)
     printf("%d", sizeof(p));
                         ^

I am a beginner in C and I tried to move p's definition into the main function, changing the definition of p, looking the error up online (none of the posts with the same error answered my question), etc., but I couldn't seem to get it to work. Any suggestions are appreciated.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Coder
  • 1,175
  • 1
  • 12
  • 32
  • 4
    `sizeof (struct p)` and use `printf ("sizeof struct p: %zu\n", sizeof (struct p));` (e.g. `p` is a struct tag name, not a declared instance of the struct, so to determine the size of the type `struct p` the entire type must be used and enclosed in parenthesis) – David C. Rankin Jan 13 '21 at 02:56
  • 1
    You can also define your struct like this: typedef struct { char x;int y;} p; and then sizeof(p) should work. This behavior is different between C and C++ so it can be confusing – Laserallan Jan 13 '21 at 02:58
  • ah thanks that works! If someone could add that as an answer ill accept, i had no idea i was missing something so simple. Btw why do you need to say `struct` before `p` in the `sizeof` call? – Coder Jan 13 '21 at 02:59

1 Answers1

4

In C (unlike in C++), the struct p... construct does not define a new type of variable. It only defines p as a particular type of struct. So, in order to get the size of that structure, or to declare a variable of that type, you need to use struct p to refer to it.

Like this:

#include <stdio.h>

struct p {
    char x;
    int y;
};

int main()
{
    printf("%zu\n", sizeof(struct p));
    // Alternatively ...
    struct p q;
    printf("%zu\n", sizeof(q));
    return 0;
}

Also, note that you should use the %zu format specifier for the size_t type.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Hmm I was just looking into the `%zu` format specifier, i saw that it mentioned `size_t`, so C has its own data type `size_t` for holding the `sizeof` of a data structure? – Coder Jan 13 '21 at 03:02
  • @JohnD Yes. See [here](https://en.cppreference.com/w/c/types/size_t). – Adrian Mole Jan 13 '21 at 03:03
  • This is probably meant for another question but seeing as it's probably trivial, the answer it prints out is `8` instead of `5` (shouldnt it be `1` byte from the `char x` and `4` bytes from the `int y`). Is this an error in the above code or is that because of padding? Do structs pad the `char x` to 4 bytes from 1 byte? – Coder Jan 13 '21 at 03:09
  • 1
    @JohnD Yes, again. The structure is padded (by default) so that the `int` member is aligned on a 4-byte boundary. You can turn that off in most compilers, or change the padding size. – Adrian Mole Jan 13 '21 at 03:12
  • `size_t` is an alias for another integer type – M.M Jan 13 '21 at 03:30
  • @JohnD See [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) – Lundin Jan 13 '21 at 09:06