As i wrestle my way to the book"teach yourself C in 21 days", more and more questions turn up in my head ...
Ok here is the question:
this is the prototype of strcpy():
the source string must be a "const char *" If i got this correct that means a pointer to a char. this pointer value cannot change, because it is const (am i right ?? ).Code:char *strcpy(char *destination, const char *source);
That would mean you only can copy an array as "source".
I wrote a small programm to test this, and the first time i use a "const char *" (array) as "source", the next strcpy i use a pointer to an array (thus not "const char *").
The output is the same both times.... Can someone explain, i'm confused.
Code:#include <stdio.h> #include <stdlib.h> int main() { /* fill the arrays with sample data, leaving room for the 0 */ char string1[10] = "abcdefghi"; char string2[10]; char string3[10] = "jklmnopqr"; /* pointer to an char */ char *arrayptr; /* this is how its suppose to be i think ... */ strcpy(string2, string1); printf("string 1 is: %s\n", string1); printf("string 2 is: %s\n", string2); /* arrayptr is not const char * */ arrayptr = string3; /*here the source is not const char * */ strcpy(string2, arrayptr); printf("string 1 is: %s\n", string1); printf("string 2 is: %s\n", string2); system("PAUSE"); return 0; }



LinkBack URL
About LinkBacks



