send pointer to function

So I'm trying to learn c++ but i cant really get a grip on pointers and reference.
anyway I'm trying to write a function that removes spaces from an char array. this is what i think i want to do

I want to send a reference of an char array to the function, then i want to go threw the array and copy every charter that isn't space to a temp array and at last set the first array to the second array.

here is what i got. can someone help me?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void removeSpace (char & line, int size) {
    char tmp[size];
    int j=0;

    for(int i=0; i<size; i++) {
        if(!isspace(line[i])) {
            tmp[j]=line[i];
            j++;
        }
    }
    line = tmp;
}
int main () {
    char line1[]={"some input from the user"};
    removeSpace(line1, strlen(line1));
    
    return 0;
}
Last edited on
void removeSpace (char* line, int size)
or
void removeSpace (char line[], int size)

might want to use std::string to make things easier.
#include <string> // std::string
okey i will try that.
Yeah i know but i want to do it with arrays.

EDIT:
one question how do i change the original array(the one in main function) if i dont use the reference?

Thanks
Last edited on
strlen is c style programming .

sizeof is c++ style. sizeof counts bytes. since char are 1 byte.
removeSpace(line1, sizeof(line1));
might want to use std::string to make things easier.


+1 to this. C strings are hairy and dangerous.

one question how do i change the original array(the one in main function) if i dont use the reference?


Arrays are passed by pointer. It's similar to passing by reference. You're not copying the entire array and passing the copy to the function. Instead, you're passing a pointer to the function (telling it where it can find the original array). Since the function has a pointer to the original array, any changes it makes to that array will change the original array.

So basically, you can strcpy() your 'tmp' to 'line' and it'll work.. except for the fact that 'tmp' isn't null terminated.

strlen is c style programming . sizeof is c++ style. sizeof counts bytes. since char are 1 byte.


Eh.

strlen is for C strings. Since he's using C strings, it makes sense here.

sizeof is just as much "C style" as as strlen is.

Although he probably should be using sizeof here (or at least strlen()+1) because he needs the extra byte for the null terminator, which he currently doesn't have.
okey first of thanks for the replays.

Arrays are passed by pointer. It's similar to passing by reference. You're not copying the entire array and passing the copy to the function. Instead, you're passing a pointer to the function (telling it where it can find the original array). Since the function has a pointer to the original array, any changes it makes to that array will change the original array.


if i understand this correct this would mean that if i wrote

line = tmp;

line1 in main would take the value of line, witch i tried and it doesn't.

sizeof is c++ style. sizeof counts bytes. since char are 1 byte.


that works much better before when I printed it in the removeSpace function i got a lot of weird charters after, probably because the new sting is sorter then the length of the string.


+1 to this. C strings are hairy and dangerous.


Yeah i get that string is better to use, why else would they add it =). but i figure char [] is mutch harder to use so if i understand that i would have a better understanding how the class string works =)
if i understand this correct this would mean that if i wrote

line = tmp;

line1 in main would take the value of line, witch i tried and it doesn't.


That doesn't work because line is a pointer. Here's an example:

1
2
3
4
5
6
7
int ar1[3] = {1,2,3};  // two arrays
int ar2[3] = {4,5,6};

int* ptr;  // a pointer
ptr = ar1;  // make it point to the first array

ptr = ar2;  // make it point to the second array 


Notice that NONE of this code changes ar1 or ar2. ar1 is still {1,2,3} and ar2 is still {4,5,6}; What we're changing here is which array the pointer points to.

This is what your function is doing. You have a pointer 'line' that points to 'line1' (your array in main).

If you say line = tmp; you're not changing line1 at all, instead you're just changing 'line' so that it points to 'tmp' instead of to 'line1'.

To copy the string from 'tmp' to 'line', you need to copy each character. There's a standard function strcpy that does this for you, but it requires that your string be null terminated (has a 0 character to mark the end of the string).

So you need to do 2 things:

1) put a null terminator at the end of your 'tmp' string
2) copy tmp to line with strcpy


Also -- it's worth noting that you don't even need to pass a 'size' parameter here. You can tell where the end of the C string is by finding the null terminator. That is how all the functions like strcpy, strlen, etc work. They just step through the string looking for the null.
thanks now i get it, and it works. but i didn't have to put a null character at the end. one thing thou i put a '\0' at line1[10] just to check what would happend and yeah it stoped printing at 10 but i also checked the sizeof and that was still 25

yeah yeah thanks a bunch.
thanks now i get it, and it works. but i didn't have to put a null character at the end


YES YOU DO

This is exactly why C strings are so dangerous. You're getting "lucky" (or really, unlucky) in that your RAM just happens to be zero'd.

You could very well run that same code on another machine and have it crash because it went out of bounds on a strcpy because you forgot to put the null at the end.
okey i still did
1
2
3
4
5
6
7
8
  
for(int i=0; i<size; i++) {     
    if(!isspace(line[i])) { //if char isn't a space copy char to temp array
            tmp[j]=line[i];
            j++;
        }
    }
    tmp[j] = '\0';

but i tried without and i guess i was lucky then
Last edited on
good good.

That null is very important.

*thumbs up*
To expand on what Disch is saying, there has to be a null terminator at the end if it isn't there, then it will skip along looking at every memory location until it finds one, the one it finds could be 500+ mb away, in which case it shows or acts as though the string is now 500mb long. Which will give you a heck of a lot of junk data at the end of the string.
You probably just got lucky in the fact that the next location in memory after the variable was a null.
Last edited on
what's worse is when you strcpy all that crap into a buffer that's not near large enough to hold it.
in which case it shows or acts as though the string is now 500mb long


That doesn't sound that good =). guess I was really lucky then ;)

what's worse is when you strcpy all that crap into a buffer that's not near large enough to hold it.


What could happens then?
Topic archived. No new replies allowed.