Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ char *test = (char*)malloc(64 * sizeof(test)); strcpy(test, "this is a test of the char pointer"); printf("%s\n",*test); return 0; }
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ char *test = (char*)malloc(64 * sizeof(test)); strcpy(test, "this is a test of the char pointer"); printf("%s\n",*test); return 0; }
Turning up your warning levels and compiling leads to a clue:
Code:seg.c:9:19: warning: format specifies type 'char *' but the argument has type 'char' [-Wformat] printf("%s\n",*test); ~~ ^~~~~ %c 1 warning generated.
Apart from that, you have the idiom for using malloc() wrong.
1) No explicit type conversion (aka typecast).Code:whatever_type *test = malloc(number*sizeof(*test));
2) The argument of sizeof() needs to be related to what is pointed at, not the pointer.
Thank you guys very much. I really appreciate it.