when i run this, i get an error, am i using pointers incorrectly? Code: void print_array(char *s) { s = (char *)malloc(6); s[0] = 'T'; s[1] = '\0'; printf("%s",s); } main() { char *word; print_array(word); printf("%s",word); // fails on this line }
obviously you are using the pointers incorrectly...you better know that the pointers are needed to be allocated dyanamically if you use it for arrar purpose...this thing you did in the function part but when it returns from the function it loses its scope. If you want to be error free try to use pointer of a pointer then only you can utilize the concept of pass by address. The array construction would remain intact even after returning from the function.