1

I meet a problem with the char array size. I pass an char array into the function and after run the function, I still want to use sizeof to check the size of the array, it won't give me the new size of the array, but the old size? I would like to know why? Thank you very much!

#include<iostream>
using namespace std;

void replacement(char* arr, int len){
   int count=0;
   for(int i=0; i<len; i++){
     if(arr[i]==' '){
       count++;
     }
   }
   int newlen=count*2+len;
  //arr[newlen]='\0';
   int k=newlen-1;
   for(int i=len-1; i>=0; i--){
     if(arr[i]!=' '){
        arr[k--]=arr[i];
     }
     else{
       arr[k--]='0';
       arr[k--]='2';
       arr[k--]='%';
     }
   }
}


int main(){
  char arr[]="ab c d e  g ";
  cout<<sizeof(arr)<<endl;
  replacement(arr, sizeof(arr));
 int i=0;
  while(arr[i]!=NULL) cout<<arr[i];  

}
Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
bunny
  • 1,797
  • 8
  • 29
  • 58

4 Answers4

3

You can't change an array's size. If you want to know the length of the string in the array, use strlen() -- this counts the number of characters before the null terminator.

Even better would be to use C++ std::string class.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Yes, and since he is using iostream he is in C++ land for sure. – Csaba Toth May 06 '13 at 23:40
  • 1
    @CsabaToth He also has `using namespace std`. – Barmar May 06 '13 at 23:41
  • I would like to know why I cannot change the array size? I have areadly change the array, should the compile check the where the null character and calculate the size of the array using sizeof()? – bunny May 06 '13 at 23:51
  • 1
    Because the size of the array is determined by the code that calls your function `replacement`. The function `replacement` can not change what has happened before it got called [in fact, the return address for your `replacement` function will be where your expanded array would need to be placed - or if it were to grow the other direction, the return address of `main`. Those are some of the technical difficulties - of course, that's just exlaining some of the reasons WHY the standard doesn't allow it. Stanards are like laws, arguing for or against doesn't usually change the law or standard. – Mats Petersson May 06 '13 at 23:57
2

Right, so you are trying to replace spaces with "%20", right?

Since C++ (or C) doesn't allow an existing array to be resized, you will either need to have enough space in the first place, or use an array allocated on the heap. Then allocate a new "replacement" string in the replacement function and return that.

The proper C++ method of doing this is of course to use std::string, in which case you could just pass it in as a reference, and do the replacement in the existing variable:

void replacement(std::string* str, int len){
   std::string perc20 = "%20";
   std::string space  = " ";
   while((pos = str.find(space, pos)) != std::string::npos)
   {
     str.replace(pos, space.length(), perc20);
     pos += perc20.length();
   }
}

Much easier...

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

You can use sizeof() to find the size of only static arrays when the size is known at compile time. Hence it will always return the size of the array as determined at compile time.

SNce
  • 2,453
  • 1
  • 16
  • 14
0

Your program technically has Undefined Behavior because your use of sizeof returns the size in bytes of your char array. But a char implicitly contains a null byte \0. That means the for loop is iterating 1 past the length of the array.

It's recommended that you use std::string along with its size member function instead.

David G
  • 94,763
  • 41
  • 167
  • 253