0

I am having some trouble with some work I was assigned for my schooling. I was told to write a program that modifies a char. I can do that just fine. I just cannot get how to work with pointers. I've researched this, and found some solutions. But they don't meet the assignment's needs. I was told to define a function exactly like this:

int stripWhite(char *userInput)

So I did. The only problem is, I believe this makes a copy of the value. And does not modify the actual value. So here is some code picked out of my program:

Inside main, I declare my char, and use cin.getline to collect my input. Then I call stripwhite. Then I cout my char. The value never changes. The assignment says stripwhite needs to be defined exactly as it is above. I am at a lose here. Any help?

char userInput[100];
int spaces = 0;
cin.getline(userInput,99);
spaces = stripWhite(userInput);
cout << userInput;
3
  • Could you post the definition of int stripWhite(char *userInput) ? Commented May 13, 2013 at 5:30
  • Since this is a school assignment, I will not post the full code, just my debug code. I simply had it do this: userInput[0] = 'a'; Commented May 13, 2013 at 5:32
  • If you posted it we could tell you what you did wrong and why it didn't work etc. without giving you the exact answer to the assignment. Commented May 13, 2013 at 5:34

3 Answers 3

4

stripWhite is defined to receive a pointer to char, which does not make a copy of the original data -- it passes a pointer to the original data, so modifying *userInput inside of stripWhite will modify the original string whose address was passed.

Simple demo:

#include <iostream>

void modify(char *input) { 
    *input = 'a';
}

int main() { 
    char data[] = "ABCD";

    std::cout << data << "\n";
    modify(data);
    std::cout << data << "\n";
    return 0;
}

Result:

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

Comments

1

Your function takes a character pointer. Based on the function name and usage, this function is presumably intended to take a character buffer as input and remove all the spaces while returning the number of spaces removed.

So basically, you have to iterate through the buffer, and essentially move down the contents while skipping spaces. The easiest way to do this is to maintain two pointers and copy from one to the other.

Here's how I'd do it (warning, not tested). Obviously since it's an assignment you'll want to write your own code, but hopefully this example will clear up any misconceptions you have.

int stripWhite(char* userInput){

    auto inpos = userInput;
    auto outpos = userInput;
    int count = 0;

    while(*inpos){
        if(*inpos != ' '){
            *outpos = *inpos;
            ++outpos;
        } else {count++;}
        ++inpos;
    }

    *outpos = '\0';
    return count;
}

1 Comment

This works. Thank you. It did clear it up for me. Pointers can be a hassle for me. Thanks once again. I'll write the code on my so I don't feel like I cheated.
0

You would make your function to receive pointer to pointer to char and change all content if you want.

char stripWhite(char **ptr){
*ptr = "String";
}

char *string;
stripWhite( &string );
cout<< string <<endl;

Comments

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.