1

So, I'm trying to write a function that takes a string and changes all lowercase values to uppercase. Here's the code:

void lowerToUpper(char *s)                                                      
{                                                                               
    char *p;                                                                                                                                      

    for (p = s; *p; p++)                                                        
    {                                                                                                                                               
         if (islower(*p))                                                                             
             *p = toupper(*p);                                                                                                                                                                               
    }                                                                           
}                                                                               

int main (int argc, char * argv[])                                              
{                                                                               
     char *pa;                                                         

    pa = "This is a test.";                                                                                                

    printf("The following string will be edited:\n");                          
    printf("%s\n%s\n%s\n", pa);                                         

    lowerToUpper(pa);                                                                                                                      

    printf("The string has been edited, and is now as follows:\n");          
    printf("%s\n%s\n%s", pa);                                           

    return EXIT_SUCCESS;                                                        
}

The problem arises from the line "*p = toupper(*p);", which is where I get a segmentation fault. My guess is that the problem arises from trying to assign the value that toupper(*p) returns to *p. After doing some tests, it seems as though toupper(*p) works, but as soon as I try to assign the value to *p, I seg fault? Any ideas as to why this would happen?

2 Answers2

7
 pa = "This is a test.";

In the above line you are setting the pointer pa to pointing to a read-only character string. When your function writes to that memory, you are invoking undefined behavior. (If you compile your program with the -Wall flag you will get a warning telling you about the problem)

A correct way to get a write-able char array would be:

char pa[] = "This is a test.";

Also, your calls to printf() specify four %s tokens, but you are only supplying a single string-pointer argument. You need to either remove three of the %s tokens, or add additional string-pointer arguments after pa.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Ah, yes, the other %s tokens are leftover from my original code which had more tests. My mistake. As for the pointer assignments, your advice worked. Thanks a million! – user3280527 May 15 '14 at 20:01
0

Change:

   pa = "This is a test.";

to:

   pa = strdup("This is a test.");

Change:

   printf("%s\n%s\n%s\n", pa);

To:

   printf("%s\n", pa);

And, perhaps 'free(pa)' should be called prior to the 'return' statement.

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28