View Single Post
Old Apr 16th, 2005, 4:10 AM   #1
conbrio
Newbie
 
Join Date: Apr 2005
Posts: 10
Rep Power: 0 conbrio is on a distinguished road
pointers strings, and strcat()

okay, i'm new to C and going crazy trying to understand this little problem.

in the code below, example A doesn't work, while B does. why is this so?

i thought that declaring strings by "char *blah" and "char blah[]" would essentially be the same since arrays are just pointers anyway. am i wrong?

the reason i ask this is because i'm writing a function that needs to use strings which aren't initialized with a value and hence need to have the array size automatically known ("char *blah;" works for this while "char blah[];" gives an array size unknown error).

after all this, i then want the strings to be passed to strcat() for concatenation.

i really hope someone can help here.. thanks.

// EXAMPLE A
#include <stdio.h>
#include <string.h>



main()
{
   char *yr="1985";
   char *car=" toyota";
   strcat(yr, car);
   printf("%s", yr);
}



// EXAMPLE B
#include <stdio.h>
#include <string.h>


main()
{
   char yr[]="1985";
   char car[]=" toyota";
   strcat(yr, car);
   printf("%s", yr);
}
conbrio is offline   Reply With Quote