This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
What is the difference between char *str and char str[] ?
- To: <gcc-bugs at gcc dot gnu dot org>
- Subject: What is the difference between char *str and char str[] ?
- From: "Markus Virtanen" <Markus at colorplaza dot com>
- Date: Wed, 25 Apr 2001 16:04:13 +0200
- Organization: Colorplaza SA
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;
}