const char *pointerStr[]=
{
"BEST123, ", // 0x00
"Best2233, ", // 0x01
"ABCDEFGH, ", // 0x02
"123456, ", // 0x03
"helloworld, " // 0x04
};
typedef struct
{
char value;
char name[40];
}StrInfo;
typedef struct
{
int regMax;
StrInfo info[60];
} structNew;
void main()
{
int i;
structNew pret;
for ( i=0;i<5;i++)
{
printf("PointerStr size of %dth %d \n",i,sizeof(pointerStr[i]));
printf("pret size of %dth %d \n",i,sizeof(pret.info[i].name));
}
}
The above program produce the result of
PointerStr size of 0th 4
pret size of 0th 40
PointerStr size of 1th 4
pret size of 1th 40
PointerStr size of 2th 4
pret size of 2th 40
PointerStr size of 3th 4
pret size of 3th 40
PointerStr size of 4th 4
pret size of 4th 40
If I want to know the size of each and every string in PointerStr then how to find it? Is it possible only using strlen ? do we have some other way ? How this variable length array is stored in memory ?
The result is becoz that pointerStr is pointer variable and its size is always 4. Please correct me if I am wrong.
The length of a C string is implicit: it is determined by where the terminating
'\0'is in the string, so you will need a function likestrlento determine the length of a string.The values ‘returned’ by
sizeofare determined by the compiler by looking at the type of the data, instead of the data itself.