Issues with dynamically allocating a string array
Posted
by
Jason Block
on Stack Overflow
See other posts from Stack Overflow
or by Jason Block
Published on 2012-07-09T09:12:37Z
Indexed on
2012/07/09
9:15 UTC
Read the original article
Hit count: 170
Brand new to C. I am trying to dynamically allocate the array frags2 of size numberOfFrags and copy over the contents of the original array to it. I have tried numerous approaches and searching and do not understand what is going wrong here. Sizeof on the new array returns 0 instead of what I thought I malloc'd. Any help would be much appreciated!
int main(int argc, const char* argv[]) {
char* frags[MAX_FRAG_COUNT];
FILE* fp = fopen(argv[1], "r");
int numberOfFrags = ReadAllFragments(fp, frags, MAX_FRAG_COUNT);
fclose(fp);
char** frags2 = (char**)malloc(numberOfFrags * sizeof(char*));
for (int i = 0; i < numberOfFrags; i++) {
frags2[i] = frags[i];
}
qsort(frags2, sizeof(frags2) / sizeof(char *), sizeof(char*), cstring_cmp);
© Stack Overflow or respective owner