> I need to allocate 10 strings, and each string is 5 characters long. I used the following but did not seem to work.
It seems to work for me, what did you try?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char (*lines)[5];
lines = malloc(10 * sizeof(*lines));
strcpy(lines[0],"test");
strcpy(lines[1],"word");
strcpy(lines[2],"hey");
strcpy(lines[3],"it");
strcpy(lines[4],"does");
for ( int i = 0 ; i < 5 ; i++ )
printf("%s ", lines[i] );
printf("\n");
free(lines);
return 0;
}
$ valgrind ./a.out
==4832== Memcheck, a memory error detector
==4832== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4832== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==4832== Command: ./a.out
==4832==
test word hey it does
==4832==
==4832== HEAP SUMMARY:
==4832== in use at exit: 0 bytes in 0 blocks
==4832== total heap usage: 1 allocs, 1 frees, 50 bytes allocated
==4832==
==4832== All heap blocks were freed -- no leaks are possible
==4832==
==4832== For counts of detected and suppressed errors, rerun with: -v
==4832== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)