简体   繁体   English

与C中的数组大小混淆

[英]Confusion with array size in C

Why does the sizeof (array [0]) return size as 8 when it has 17 characters/bytes within it? 当sizeof(数组[0])中包含17个字符/字节时,为什么返回size为8?

#include <stdlib.h>
#include <stdio.h> 

int main  () {

  char* array [2] = {"12345678912345678","12345678"};
  int a = sizeof  (array [0]);

  fprintf (stdout, "%s , %d \n", array [0], a);

  return 0;
}

Returns: 返回:

12345678912345678 , 12345678

To get the length of the actual character string being stored, use 要获取要存储的实际字符串的长度,请使用

printf("%lu", strlen(array[0]));

This gives 17 . 这给出了17

Make sure to include the <string.h> header. 确保包含<string.h>标头。

sizeof(array [0]) returns the size of the pointer which is depend your system. sizeof(array [0])返回指针的大小,该大小取决于您的系统。 Use strlen() function to get your expected output. 使用strlen()函数可获得预期的输出。

  int a = strlen(array [0]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM