-2

I'm trying to find out the size of structure variable. As per the code sizeof structure should be 28, but the result shows 8 and name is given more than 20 characters, but it is printed fine. I'm unable to understand this behaviour, can anyone help me out?

Iam using DevC++ to compile my C code.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct
{
    char name[20];
    int identity;
    int number;
}S;

int main()
{
    S *s=(S *)malloc(sizeof(S));
    printf("Size of structure is %d \n",sizeof(s));
    strcpy(s->name,"abcdefghijklmnopqrstuvwxyzffjfjfzdabcdefg");
    printf("name is %s and size of name is: %d\n", s->name ,sizeof(s->name));
    s->identity=10;
    s->number=20;
    printf("Identity is %d and size of identity is %d\n",s->identity, sizeof(s->identity));
    printf("Number is %d and size of number is %d\n",s->number , sizeof(s->number));
    return 0;
}

I expect the output of "Size of structure" to be 28,but the actual output is 8.

  • 3
    Because you print size of pointer, not structure. –  Dec 30 '18 at 13:30
  • Although unrelated to your current problem, [this question](https://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member/119128) should help answer a possible future question. – Some programmer dude Dec 30 '18 at 13:32
  • And an important note: Writing out of bounds of an array (like you do with that `strpy` call) leads to [*undefined behavior*](https://en.wikipedia.org/wiki/Undefined_behavior)! Never do it! And no, C does *not* have any kind of bounds-checking. – Some programmer dude Dec 30 '18 at 13:34
  • "... name is given more than 20 characters, but it is printed fine." What do you expect should have happened? – chux - Reinstate Monica Dec 30 '18 at 14:14

2 Answers2

1

s does not have type S. It has type S *, i.e. it is a pointer, so sizeof(s) is the size of a struct pointer on your system.

If you want to print the size of the structure, you would need to print sizeof(S) or sizeof(*s).

Also, you're using the wrong format specifier to print the result of sizeof. You're currently using %d in each of your printf calls, whereas the proper format specifier is %zu for sizeof.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

Here is my observation, firstly here

S *s=(S *)malloc(sizeof(S));

memory is allocated for s which is a pointer of structure S and printing sizeof(s) doesn't result in size of whole structure, it just print the size of structure pointer.

If you want to print size of complete structure, either use sizeof(S) or sizeof(*s).

secondly, using %d format specifier to print sizeof() operator result causes undefined behavior. Use %zu. For e.g

printf("Size of structure is %zu \n",sizeof(*s));

Also here

strcpy(s->name,"abcdefghijklmnopqrstuvwxyzffjfjfzdabcdefg");

s->name can contains only 20 characters including terminating \0 characters & copying more than size characters causes undefined behavior. Since C doesn't perform array boundary check, doesn't mean that one should write code which access out of bound data.

Achal
  • 11,821
  • 2
  • 15
  • 37