Reputation:
There is my code why the size of struct is differs from size of struct pointer
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct flx
{
int age;
int id;
char name[];
}*s;
int main()
{
char name[50]="Hi how are you";
s=malloc(sizeof(s)+sizeof(char)*strlen(name));
s->id=12345;
s->age=16;
strcpy((*s).name,name);
printf("Id --- %d\nName --- %s\n age --- %d",s->id,s->name,s->age);
printf("\nTotal Sizeof *s is %d \nSize of s is
%d",sizeof(*s),sizeof(s));
return 0;
}
output:(In code::blocks)
Id --- 12345
Name --- Hi how are you
age --- 16
Total Sizeof *s is 8
Size of s is 4
Process returned 0 (0x0) execution time : 0.473 s
Press any key to continue.
1)In above the code printed total sizeof struct is 8 and size of pointer is 4 this is why? 2)But when i compile the following code
#include<stdio.h>
int main()
{
int *b;
printf("sizeof b is %d\nSize of *b is %d",sizeof(b),sizeof(*b));
}
The output shows
sizeof b is 4
Size of *b is 4
Process returned 0 (0x0) execution time : 0.475 s
Press any key to continue.
so why the sizeof struct shows different size and what is the reason to show this "Size of s is 4"?
Upvotes: 1
Views: 686
Reputation: 123448
There is no relationship between the size of a pointer type and the size of the type it points to. That sizeof (int)
and sizeof (int *)
yield the same value on your system is coincidence (they don’t on my system).
A pointer stores an address (either physical or virtual) - on a system like x86 with a flat memory model, all pointer types are the same size (usually the native word size, either 32 or 64 bits on most desktop systems). However, there are some oddball architectures out there where pointer types may have different sizes from each other.
The only requirements1 are:
char *
and void *
have the same size and representationint *
and const int *
and volatile int *
have the same size)struct
pointer types have the same size and representation;union
pointer types have the same size and representation;Nothing is guaranteed beyond that.
Upvotes: 1
Reputation: 47923
A pointer is almost always a different size than the data it points to, for the simple and fundamental reason that a pointer is different (it's different, period) than the data it points to.
A pointer is not the data. A pointer is a pointer to the data. A pointer is not a copy of the data, either. (One important use of pointers is that, since generally they're much smaller, it's more efficient to copy pointers around, leaving the pointed-to data in place, rather than copying the data around.)
Possibly meaningless analogy: I am almost 7 feet tall, and weight about 240 pounds. Yet my name, when I write it on a piece of paper, is on the order of 8½ × 11 inches, and weighs less than an ounce. Why is Steve Summit's name a different size and weight than Steve Summit?
Upvotes: 0
Reputation: 222362
A structure, an int
, a pointer to a structure, and a pointer to an int
are all different types. There is no reason they must be the same size.
In many C implementations, a pointer to a structure is the same size as a pointer to an int
, but this is not required by the C standard.
In many implementations, a pointer is the same size as an int
because both are influenced by features of the target processor architecture, but this is not required by the C standard.
However, the sizes of structures vary greatly, as their sizes depend on their contents. In contrast, the sizes of pointers to structures do not depend on the contents of the structures, any more than the length of a postal address of a house depends on the size or contents of the house.
Upvotes: 2