-3

I created the following array:

 char* myarr= "data successfully inserted";

when I do

sizeof(myarr)

I get 8. Why do I get that?. If 1 char is 1 byte then size of myarr should be 26 by just counting the characters right?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
daniel
  • 557
  • 1
  • 4
  • 15

3 Answers3

4

On your platform, a char pointer is 8 bytes. That's the size of the pointer variable, not the size of whatever it's pointing to. If you want the length of a string, call strlen(myarr).

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

Because that's the size of a pointer on your system. If you want to get the length of a string, you should use strlen() function.

msc
  • 33,420
  • 29
  • 119
  • 214
0

sizeof(myarr) is not the size of the string but the size of char * pointer on your platform which gives you 8 bytes. the size of the array can be calculated by strlen(myarr)

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400