0

I have a char pointer that holds a single character. I want to create a while loop that will continue to run as long as that character is not '~'. But I cannot seem to get the characters to compare correctly.

char *currChar;
while(currChar != '~') {
    //read();
    printf("Current Char:%c\n", currChar);
}

read puts the last value entered by the user into currChar. The printF will always output the correct information (i.e. a ~ if the user pressed ~) but for some reason he while loop will never be exited.

If i try to do something like while(*currChar != '~') I get a segmentation fault error.

What can I do to get this to work?

Thanks in advance.

Duffluc
  • 27
  • 2
  • 6

3 Answers3

1

Your char pointer does not hold a single character. It points to a single character in memory. You need to declare an actual char somewhere.

E.g.,

char currChar = ' ';  /* Initialize to something not '~' */
while( currChar != '~' ) {
    /* read().  If your read function requires a pointer, then provide &currChar. */
    printf("Current character: %c\n", currChar);
}

So what's wrong with your code is that char *currChar; creates an uninitialized pointer. That pointer is not pointing anywhere. You could do this:

char currChar = ' ';
char *currChar_ptr = &currChar;

Now you have a character. And your pointer is pointing to that character.

indiv
  • 17,306
  • 6
  • 61
  • 82
  • This is an infinite loop – Ryan Nov 18 '14 at 00:21
  • @self: It's pseudo code copied directly from the OP's pseudo code. The idea is that in the commented code area, there is a function that reads a character. – indiv Nov 18 '14 at 00:22
0

You need to compare *currChar. But the reason you get a segfault is because the first time you compare *currChar to '~', you are comparing an undefined pointer to '~'.

So call read(); before you enter the loop, and make sure you allocate space to store your char in the first place.

Or:

char *currChar = '_';

do {
    // read();
    printf("Current Char:%c\n", currChar);
} while (*currChar != '~');
Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59
stromdotcom
  • 381
  • 1
  • 8
  • The read() method call is from the code he posted. Neither you nor I know what this read() is or whether it should or should not have arguments. – stromdotcom Nov 18 '14 at 00:40
  • 2
    Its irrelevant to the question. He already said his `read()` method is working correctly. – stromdotcom Nov 18 '14 at 00:42
  • `read()` was commented out of the code OP posted, but you think it's relevant, so explain it. – Weather Vane Nov 18 '14 at 00:42
  • And? Why is this confusing to you? His issue was how to compare a char* to a char literal, not how to read a char from input. – stromdotcom Nov 18 '14 at 00:46
  • 1
    Stop bickering. This is not helpful to either you or the OP. I just commented out read() to make it match the original question. – Alex Weinstein Nov 18 '14 at 01:09
0

You have to read the user input before you enter the loop.

This code works:

char *currChar;
do{
    read();
    printf("Current Char:%c\n", *currChar);
    currChar++;
}while(*currChar != '~') 
Ludovic Feltz
  • 11,416
  • 4
  • 47
  • 63