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];  

}
4
  • 1
    possible duplicate of Sizeof array passed as parameter Commented May 6, 2013 at 23:34
  • 1
    @MatsPetersson No, this is different, he's not using sizeof on a parameter. Commented May 6, 2013 at 23:35
  • while (arr[i] != NULL) cout << arr[i]; is an infinite loop. Commented May 6, 2013 at 23:35
  • Ah, sorry, misread the question. Commented May 6, 2013 at 23:36

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, and since he is using iostream he is in C++ land for sure.
@CsabaToth He also has using namespace std.
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()?
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.
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...

Comments

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.

Comments

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.

2 Comments

I need to use a c string. Thank you.
@dianedan sorry I meant sizeof(arr)/sizeof(char).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.