I have tried to implement a dynamic array of strings, which asks the
user for number of elements in the array, then allocates memory for the
array and each element in it. The working program is pasted below.
I'd like some feedback if this is in good form and potential pitfalls
that could be present in my implementation (e.g. memory leaks, dangerous
casts etc.). And is there a better way to do this?
int main(void)
{
long *rgWords;
char *lpszWord;
unsigned int c = 0;
int i;
while (c < 1)
{
printf("num [>0]");
scanf("%d", &c);
}
rgWords = (long *) malloc(sizeof(long) * c);
for (i = 0; i < c; i++)
{
lpszWord = (char *) malloc(sizeof(char) * 10);
printf("?");
scanf("%s", lpszWord);
*(rgWords + (i * sizeof(long))) = (long)lpszWord;
}
for (i = 0; i < c; i++)
{
printf("%s\n", *(rgWords + i * 4));
}
}
Regards,
Pranav Negandhi
www.pranavnegandhi.com


|