Your code is totally messed up in every aspect!
1) you allocated memory for exactly 1 Pointer to it. This means you can access ptr[0], but not ptr[1] ... ptr[4] as you are trying to do.
2) you never allocate anything for the elements in ptr[i].
3) you try to print a string a ptr[i] which is (even if your allocation would be right) never terminated.
4) although this is obviously only a beginners test, never forget to free your memory!!!!
To reach something CLOSE to what your sampel code is describing you could do:
int main() { int i,j; char ** ptr = malloc( 5 * sizeof(char*) ); /* an array of 5 elements of type char* */ for(i = 0; i < 5; i++) { ptr[i] = malloc( 11*sizeof(char) ); /* The element i of the array is an array of 11 chars (10 for the 'a' character, one for the null-termination */ for(j = 0; j < 10; j++) ptr[i][j] = 'a'; ptr[i][10] = '\0'; /* strings need to be null terminated */ printf("%s\n", ptr[i]); } // free your memory! for (i=0; i<5; i++ ) { free(ptr[i]); } free(ptr); return 0;
ptr[i]is a pointer, but what does it point to ?char * ptr = (char *) malloc(sizeof(char))and take away the second loop statement, it works fine.ptrdoes, butptr[0]does not.malloc& friends in C!