简体   繁体   中英

Confusion with array size in C

Why does the sizeof (array [0]) return size as 8 when it has 17 characters/bytes within it?

#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 .

Make sure to include the <string.h> header.

sizeof(array [0]) returns the size of the pointer which is depend your system. Use strlen() function to get your expected output.

  int a = strlen(array [0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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