I am trying to create an array of strings in C using malloc
. The number of strings that the array will hold can change at run time, but the length of the strings will always be consistent.
I've attempted this (see below), but am having trouble, any tips in the right direction will be much appreciated!
#define ID_LEN 5
char *orderedIds;
int i;
int variableNumberOfElements = 5; /* Hard coded here */
orderedIds = malloc(variableNumberOfElements * (ID_LEN + 1));
Ultimately I want to be able to use the array to do this:
strcpy(orderedIds[0], string1);
strcpy(orderedIds[1], string2);
/* etc */
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…