Hi,
I'm asking this question out of curiosity rather than neccessity:
E.g:
------
//Vars
char foo[20] = "abcd";
char *bar = "ghjk";
a) scanf("%s",bar); /*doesn't work; compiles but crashes (using g++) */
b) strcat("qwerty",foo); /* ditto, although foo shouldn't change */
c) strcat(bar,foo); /*" "; " " "*/
strcpy(...); /* ... */
d) strcat(foo,bar); /*works, no problem*/
strcpy(...); /* ... */
-----
The same "problem" arises when using other string functions such as strcpy etc. Also, why can't you "scanf" into a char *, only into a char * constant (array name)?
Is this because "bar" is a pointer and "foo" a constant pointer?
I'm just very curious to know why and can't seem to figure it out.
Many thanks in advance
Protonman
I'm asking this question out of curiosity rather than neccessity:
E.g:
------
//Vars
char foo[20] = "abcd";
char *bar = "ghjk";
a) scanf("%s",bar); /*doesn't work; compiles but crashes (using g++) */
b) strcat("qwerty",foo); /* ditto, although foo shouldn't change */
c) strcat(bar,foo); /*" "; " " "*/
strcpy(...); /* ... */
d) strcat(foo,bar); /*works, no problem*/
strcpy(...); /* ... */
-----
The same "problem" arises when using other string functions such as strcpy etc. Also, why can't you "scanf" into a char *, only into a char * constant (array name)?
Is this because "bar" is a pointer and "foo" a constant pointer?
I'm just very curious to know why and can't seem to figure it out.
Many thanks in advance
Protonman