Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Feedback needed...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 4236 of 4370
Post > Topic >>

Feedback needed - Dynamic array implementation

by Pranav Negandhi <pranav@[EMAIL PROTECTED] > Aug 5, 2008 at 10:42 AM

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
 




 5 Posts in Topic:
Feedback needed - Dynamic array implementation
Pranav Negandhi <prana  2008-08-05 10:42:07 
Re: Feedback needed - Dynamic array implementation
Richard Heathfield <rj  2008-08-05 07:09:09 
Re: Feedback needed - Dynamic array implementation
Pranav Negandhi <prana  2008-08-05 23:32:49 
Re: Feedback needed - Dynamic array implementation
Ian Collins <ian-news@  2008-08-06 15:57:15 
Re: Feedback needed - Dynamic array implementation
Ulrich Eckhardt <dooms  2008-08-05 19:31:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Nov 21 10:58:49 CST 2008.