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 > Compilers LCC > Re: Structures ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 951 of 1070
Post > Topic >>

Re: Structures and malloc at runtime

by CBFalconer <cbfalconer@[EMAIL PROTECTED] > May 31, 2007 at 05:41 PM

bigpig@[EMAIL PROTECTED]
 wrote:
> 
> Please could someone look at the following code and see if with
> some modifications it is possible to make it work:
> I just want to allocate some memory at runtime for 500
> structures (in this example) and use it as in the example
> 
> struct data {
>   char name[10];
>   long age;
> } ENTRY;

This declares a data item of type struct data named ENTRY.  Please
don't use upper case for data names, reserve the upper case for
macros.
> 
> struct ENTRY * list1 = (struct ENTRY *)malloc(500 * sizeof(ENTRY));

This should create a compiler error.  Replace "ENTRY" with "data". 
Also eliminate the cast.  You are obviously using C, not C++, and
the cast can only hide errors.  Have you #included <stdlib.h> ?
> 
> strcpy(list1[1].name, "Hello");    (1)
> list1[1].age = 5;
> 
> (1) will give the following errors:
> unknown size for type 'incomplete struct ENTRY defined at....
> unknown field 'name' of 'incomplete struct ENTRY defined at...
> type error in argument 1 to 'strcpy'; found 'int' expected
> 'pointer to char'

Now I expect the above code will work.

-- 
 <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
 <http://www.securityfocus.com/columnists/423>
 <http://www.aaxnet.com/editor/edit043.html>
 <http://kadaitcha.cx/vista/dogsbreakfast/index.html>
                        cbfalconer at maineline dot net



-- 
Posted via a free Usenet account from http://www.teranews.com
 




 4 Posts in Topic:
Structures and malloc at runtime
bigpig@[EMAIL PROTECTED]   2007-05-31 12:38:25 
Private Message
   2007-05-31 19:48:01 
Re: Structures and malloc at runtime
Richard Heathfield <rj  2007-05-31 19:55:27 
Re: Structures and malloc at runtime
CBFalconer <cbfalconer  2007-05-31 17:41:03 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Tue Oct 7 13:00:21 CDT 2008.