0

I know how to calculate the length for a string array in c++ (sizeof(arr)/sizeof(arr[0])), but I'm having a problem calculating the length of a string array if I pass an array through a method that's inside another class. Is there a reason why this happens?.

main.cpp

/* Allways returns 3 */

std::string arr[3];
int length = sizeof(arr)/sizeof(arr[0]);

std::cout << length << std::endl;

main.cpp but calling the method from a class

/* Allways returns 0 */

Test* test = new Test();
std::string arr[3];
test->length(arr);

Test.cpp

void Test::length(std::string arr[]){
   int length = sizeof(arr)/sizeof(arr[0]);
   std::cout << length << std::endl;
}

I have passed in the array as a reference but It's not letting me do that, I have tried converting the array into a char array and passing in the value.

  • 1
    The array decays to a pointer when you pass it to another function. Subsequently, `sizeof(arr)` is evaluated as the size of a pointer (typically but not necessarily, 4 or 8 bytes), which is most likely not the full size of the original array. BTW, you're missing `[]` at the end of `void Test::length(std::string arr)`. –  Nov 26 '22 at 22:20
  • You probably didn't understand the difference between `std::string` and a `char[]` array. I have honestly no idea what you're asking about. – πάντα ῥεῖ Nov 26 '22 at 22:20
  • Your code does not compile, did you mean `void Test::length(std::string* arr){`. – john Nov 26 '22 at 22:21
  • would you know how I could fix it so as to always have it return the true value? – Pixel Arcade Nov 26 '22 at 22:21
  • john, I fixed the typo, it's supposed to pass in an array in the method, I added the [ ] in the question – Pixel Arcade Nov 26 '22 at 22:22
  • 2
    The size of an array is always fixed. It's really not clear what you question is. I'm also wondering whether you really want a string, and not an array of strings. – john Nov 26 '22 at 22:22
  • @PixelArcade As already said there is no way to have an array as a function parameter in C or C++. It's always a pointer (strings have nothing to do with this, any array would be the same). The C++ solution is to not use an array but use a vector instead. Then it's as simple as `vec.size()` – john Nov 26 '22 at 22:24
  • @PixelArcade https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay?r=Saves_AllUserSaves – πάντα ῥεῖ Nov 26 '22 at 22:25
  • Thanks, john, I'll try converting it to a vector, that should fix it. I wanted to do something different which it's why I was using an array, but either way thanks, that helped a lot. – Pixel Arcade Nov 26 '22 at 22:26
  • std::array and std::vector ae what you should use in c++ – pm100 Nov 26 '22 at 22:30
  • Make your life simpler. Never use the built in array like `T[5]`, It's a holdover from C and has a lot of differences from the behavior of things like `std::vector` and `std::array` which are standard library objects. Instead of `std::string s[5]` use `std::array s` and you will find you can pass values or references consistently. And `std::vectors` is really the best goto to replace C style arrays. Especially when you don't know how many you need (VLA). – doug Nov 26 '22 at 22:31
  • Thanks, doug, gonna keep it in mind from now on when trying to go around arrays. – Pixel Arcade Nov 26 '22 at 22:36

0 Answers0