sizeof的工作原理如何?可变长度字符串数组的内存映射

Kni*_*t71 -1 c sizeof

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));
}
}
Run Code Online (Sandbox Code Playgroud)

以上程序产生的结果

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 
Run Code Online (Sandbox Code Playgroud)

如果我想知道PointerStr中每个字符串的大小,那么如何找到它?是否只能使用strlen?我们还有其他方法吗?这个可变长度数组如何存储在内存中?结果是becoz指针指针变量是指针变量,它的大小总是4.如果我错了,请纠正我.

小智 7

C字符串的长度是隐式的:它取决于'\0'字符串中终止的位置,因此您需要一个函数strlen来确定字符串的长度.

"返回"值sizeof由编译器通过查看数据类型而不是数据本身来确定.