Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof char* array in C/C++

Tags:

c++

c

sizeof

There are plenty of similar inquires, but in my case I don't understand what isn't working:

int mysize = 0;
mysize = sizeof(samplestring) / sizeof(*samplestring);
std::cout << mysize << '\n' << samplestring;

This outputs:

4

Press 'q' to quit.

How is it possible? 4 definitely isn't the size of this string. I even tried the following, with the same result:

mysize = sizeof(samplestring) / sizeof(samplestring[0]);

EDIT: Ok, this is the declaration:

char *samplestring = "Start."; 

I'm on C++, but I need to use functions that only accept char *. Later in the code I assign new strings to that variable, like:

samplestring = "Press 'r' for red text.";

Yes, the compiler gives me warnings, but I have no idea how can I use different strings if I can't overwrite them...

like image 332
Abalieno Avatar asked Sep 10 '12 14:09

Abalieno


People also ask

What does the sizeof a char * mean?

The sizeof keyword refers to an operator that works at compile time to report on the size of the storage occupied by a type of the argument passed to it (equivalently, by a variable of that type). That size is returned as a multiple of the size of a char, which on many personal computers is 1 byte (or 8 bits).

Can I use sizeof on a char array?

first, the char variable is defined in charType and the char array in arr. Then, the size of the char variable is calculated using sizeof() operator. Then the size of the char array is find by dividing the size of the complete array by the size of the first variable.

What is the size of a char array?

The size of a character array is the number of array elements +1 I.e If there are 8 elements it has a sizeof 9.


2 Answers

4 isn't the size of the string, because samplestring isn't a string. It's a char*, whose size is (on your platform) 4, divided by 1 (size of char) is, correctly, 4.

In C++, you'd use std::string and the length() method.

In C, you'd use strlen which takes as parameter a NULL-terminated char pointer.

like image 154
Luchian Grigore Avatar answered Sep 18 '22 23:09

Luchian Grigore


First of all, sizeof(samplestring[0]) is the same as sizeof(*samplestring), they're both returning the size of the first element of the samplestring array. And, assuming samplestring is an array of chars, sizeof(char) is defined to be 1.

You haven't shown how samplestring is declared. It could be one of the following:

char const *samplestring = "Hello, World!";

or

char *samplestring = malloc( ... );

or

char samplestring[10];

In the first 2 cases the type of samplestring is char *, so sizeof(samplestring) returns sizeof(char *), which, on your platform is 4.

In the third case, the type of samplestring is char[10] (array of 10 chars), but if you call a function that takes a char * as its parameter, the char array will decay to a pointer pointing to the first element of the array. In this case, trying to print sizeof within the function will still result in the size of a pointer being printed.

If you want the size of the original array to be printed from within the function, then the function parameter needs to be a pointer to the original array type (and the type includes size of the original array).

#include <stdio.h>

void foo( char (*arr)[42] )
{
  printf( "%u", (unsigned)sizeof(*arr) );
}

int main()
{
  char arr[42];
  foo( &arr );

  return 0;
}  

Output:

42

This fixes the size of the array that can be passed to the function and is not desirable in a lot of cases. The only other solution is to keep track of the array yourself (or use strlen if you have a NULL terminated string).

like image 35
Praetorian Avatar answered Sep 20 '22 23:09

Praetorian