What is the difference between char *str and char str[] ?

Markus Virtanen Markus@colorplaza.com
Wed Apr 25 07:06:00 GMT 2001


I have a small test program that causes seg fault if 
I define a string as: char *test = "some text";
and pass that string to a function that manipulates it. 
However if I define it as char test[] = "some string"; 
everything works fine.

Again some detail I didn't learn at University?

------- 8< ----------------------
#include <stdio.h>

/* Taken from K&R 2nd ed. p 47 */

void squeeze(char *s, char c)

{

    int i=0, j=0;


    for (i=0,j=0; s[i] != '\0'; i++) 

        if (s[i] != c) 

            s[j++] = s[i];

    }

    s[j] = '\0'; 

}

int main(void)

{

    char *test = "4444 2222 7777 8888";


    squeeze(test, ' ');


    printf("New string: %s\n", test);


    return 0;

}






More information about the Gcc-bugs mailing list