Where have I gone astray?
#include <stdio.h>
int main(int argc, char* argv[])
{
int e, size;
char** cArray;
char buffer[100];
printf("Enter Size: "
scanf("%d", &size);
(char*)cArray = malloc(size*sizeof(char*));
for(e=0;e<size;e++)
{
printf("Enter String %d: ", e+1);
scanf("%s", buffer);
cArray[e] = buffer;
}
printf("=== Strings Entered ===\n"
for(e=0;e<size;e++)
printf("%s\n", cArray[e]);
printf("\n"
free(cArray);
return(0);
}
Example Run:
prompt$ ./test
Enter Size: 4
Enter String 1: why
Enter String 2: doesn't
Enter String 3: this
Enter String 4: work
=== Strings Entered ===
work
work
work
work
prompt$
What's up with that? Obviously it's not indexing the char* but just assigning cArray to the last given string. In any case how do you manually allocate a runtime array of char* like the char* argv[] which is passed to the program?
Thanks,
bitwise
#include <stdio.h>
int main(int argc, char* argv[])
{
int e, size;
char** cArray;
char buffer[100];
printf("Enter Size: "
scanf("%d", &size);
(char*)cArray = malloc(size*sizeof(char*));
for(e=0;e<size;e++)
{
printf("Enter String %d: ", e+1);
scanf("%s", buffer);
cArray[e] = buffer;
}
printf("=== Strings Entered ===\n"
for(e=0;e<size;e++)
printf("%s\n", cArray[e]);
printf("\n"
free(cArray);
return(0);
}
Example Run:
prompt$ ./test
Enter Size: 4
Enter String 1: why
Enter String 2: doesn't
Enter String 3: this
Enter String 4: work
=== Strings Entered ===
work
work
work
work
prompt$
What's up with that? Obviously it's not indexing the char* but just assigning cArray to the last given string. In any case how do you manually allocate a runtime array of char* like the char* argv[] which is passed to the program?
Thanks,
bitwise