-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
  • 330,190
  • 66
  • 504
  • 555
daniel
  • 489
  • 1
  • 3
  • 14

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
  • 330,190
  • 66
  • 504
  • 555
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
  • 32,079
  • 22
  • 110
  • 197
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
  • 246,420
  • 52
  • 367
  • 373