user3280527
user3280527

Reputation: 11

Segmentation fault cause by toupper

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?

Upvotes: 1

Views: 1889

Answers (2)

Mahonri Moriancumer
Mahonri Moriancumer

Reputation: 6003

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.

Upvotes: 0

Jeremy Friesner
Jeremy Friesner

Reputation: 73181

 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.

Upvotes: 7

Related Questions