Search code examples
carraysfileiosizeof

the size of my text file is 25 bytes, according to ftell. However, when I use ftell as the size of a char array, sizeof gives me 200. what gives?


Essentially, this is my problem.

FILE *fp = "/my/textfile/location";
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);

char *output_string[size];

printf("%d", size); // gives me 25
printf("%ld", sizeof(output_string)); // gives me 200.

Why does sizeof(output_string) give me 200 when the int returned from ftell is 20?


Solution

  • char *output_string[size];
    

    This is array of pointer, if you are running in 64-bits machine, one pointer is 64-bits, which is 8 bytes. if size is 25, then :

    sizeof(output_string) = 25 * sizeof(char *) = 25 * 8 = 200