views:

86

answers:

3

I have the following code:

int main() {
    char *sPPhrase[51];

    /* Input */
    printf("Enter string (max. 50 chars):\n");
    fflush(stdout);                         /* Works around an annoying Eclipse bug that fails to display the output from the printf command */
    scanf("%s", *sPPhrase);   /* Won't work */

    /* More code goes here */
}

The scanf() command fails, I assume, because *sPPhrase is not writable as sPPhrase points to a string constant. The compiler doesn't have a clue of anything being wrong. A little later on, I need to pass this string to this function:

char* reverse(char* sPPhrase[]);

The string constant is not writable, but I need to pass this char* on to this function. How do I rewrite my code to make it work?

+6  A: 

You are declaring an array of pointers, not a array of chars (commonly used as a string).

You need to declare like this:

char sPPhase[51];

Also, sscanf can get you in trouble: it's better to use fgets to read a string in a bounded buffer:

int main() {
    char sPPhrase[51];
    printf("Enter string (max. 50 chars):\n");
    fflush(stdout);
    fgets(sPPhrase, 50, stdin);  // leave one byte for '\0'

    // More code
}

I don't know what 'reverse' is doing, but you should probably define it as:

char* reverse(char* sPPhrase);

If it is doing the operation in place, you don't even need a return value. If you do, don't forget to free it when you are done.

florin
And change scanf to scanf("%s", sPPhrase);
RC
Also needs to update the scanf to scanf("%s",sPPhrase)
zebrabox
Then how do I call the function? `char* sPReverse = reverse(sPPhrase);` generates this error:"passing arg 1 of `reverse' from incompatible pointer type"
Pieter
Like florin said above, it looks like your definition of `reverse` is flawed, in that it's expecting an array of pointers to char, as opposed to an array of char. Change the definition of reverse to `char *reverse(char *sPPhrase)`, so that the types match.
John Bode
A: 

Your decaration of sPPhase:

char *sPPhrase[51];

Is actually an array of 51 pointers. What you actually want is just an array of characters:

char sPPhrase[51];

When you do that, you should change the scanf

scanf("%s",sPPhrase)

Also note that your scanf might read more than you expect.

Alan H
A: 

To understand this, you need to go back to how arrays are implemented in memory. char* sPPhrase[51]; is a declaration of a pointer to pointers, which you can think of as similar to a two-dimensional array. If you declare this and call scanf to read into it, you set the value of an entire array equal to one character. This is like saying:

char chars2D[50][50];
chars2D[0] = 'A';

What this is doing is setting an entire array equal to 'A', so that the memory address of the array is 'A'. This is a garbage value in memory. When you call scanf("%s", *sPPhrase); you are just multiplying the problem by attempting to set the top of each array equal to a letter. So you get garbage.

Here is a thread describing how to use scanf to read into an array of characters.

David