0
#include <iostream>

@brief calculates and prints array size
@param string the array
void function(char* string){
int n = sizeof(string)/sizeof(char); //should be size of the array given
printf("%d",n); //prints INCORRECT size - 8
}

int main(){
char str[] = "abcd"; //size is 5
printf("%d",(sizeof(str)/sizeof(char)); //prints correct size - 5
}

The issue :

When I try to calculate the size of an array given with the formula in the code, it returns different values in main and in any function. The result in main block is correct, but in any function block, it calculates the wrong value. Why?

I tried calculating the size of an array, and was expecting to get it.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
Mark Y
  • 1
  • 2
    In that context, `sizeof(string)` will always return the size of the pointer, which will often be 4 or 8 depending upon your platform. The number of bytes in a string is gotten with `strlen(string)` unless it's not a null-terminated string. And in that case, you have a bigger problem to solve. – Jeff Holt Apr 29 '23 at 19:01
  • 1
    Does this answer your question? [Sizeof string vs sizeof string pointer](https://stackoverflow.com/questions/65747055/sizeof-string-vs-sizeof-string-pointer) – Jeff Holt Apr 29 '23 at 19:03
  • In `main` variable `str` is an array, in `function` variable `string` is a pointer. That's the reason for the difference. In other words sizeof array != sizeof pointer. – john Apr 29 '23 at 19:17
  • That's one more reason to use `std::string` instead. They know their size. – Bob__ Apr 29 '23 at 19:20
  • Yes I am aware that std::string is good to use, but the question I had to solve wanted me to do it without using std namespace. However the second comment mentioned that in main str is an array, and in a function it's a string. But isn't a string in C is an array of chars? – Mark Y Apr 29 '23 at 19:25
  • Please read [what is array to pointer decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay). – Bob__ Apr 29 '23 at 19:34
  • You get the length of a C string using `strlen(pointer)`, not `sizeof(pointer)`. – BoP Apr 29 '23 at 21:30

1 Answers1

0

As already mentioned in the comments, when you're dealing with strings, you probably want to use std::string (which can return its size in response to your calling its .size()).

If what you're dealing with really needs to be an array, you can still compute its size in a function though.

template<class T, size_t N>
size_t function(T (&array)[N]) {
    return N;
}

int main() { 
    char str[] = "abcd";

    std::cout << sizeof(str) / sizeof(str[0]) << "\n";

    std::cout << function(str) << "\n";
}

Result:

5
5

...but, I feel obliged to point out that I haven't used code like this in years. Most code I'd have once written using an array I'd now write using std::vector instead (and much like std::string, std::vector has a .size() member you can call to get its current size).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111