can tell me doing wrong here ? don't text when print printf("%s\n",text[0]);
i created char **text;
, malloc
ed pointers.
#include<stdio.h> #include<string.h> #include<stdlib.h> char **text; int main() { int i; text = malloc(20000); for(i=0; < 20000; i++) { text[i] = malloc(4); memcpy(text[i], "test",4); } printf("%s\n",text[0]); printf("%s\n",text[1]); }
i believe you're looking this:
int numelements = 20000, i; // allocate array of char pointers text = malloc(numelements * sizeof( char *)); for( = 0; < numelements; i++) { // 4 length of string "test", plus 1 additional \0 (null byte) text[i] = malloc( (4 + 1) * sizeof( char)); memcpy( text[i], "test\0", 5); }
here online demo showing working, produces output:
test test
Comments
Post a Comment