Coder's Guild Mailing List

Re: Aid with objects and Ansi C

Posted by Sharvil Nanavati on 2000-10-25

> struct user_object {
> 	char *product_name;
> 	char *prod_desc[50];
> 	char *prod_desc2[50]
> 	char *prod_id;
> 	int  price;
> 	int  item_type,item_use,item_var;
> 	OBJ_OBJECT prev, next;
> 	};
> i have that much of the code down. Yet i dont fully understand each
> section of it, and yes, i have read the K&R2 book, read documents
> online
> and i STILL dont seem to be grasping it. For example.. WHY do
> product_name
> and prod_desc need to be *product_name and *prod_desc

Well, I'm assuming product_name is a string. Now, there are 2 ways of
working with strings. Either you can have an array of characters, or
you can have a pointer to a memory location you have allocated. An
array of characters would be declared like this:

char product_name[100];

The problem with that approach is that the product_name variable will
use up 100 bytes no matter what. If the product name is "abc", it still
uses 100 bytes. If the name is "abfjlkadf;ajfdafsfafjl", it uses 100
bytes. That's a waste of space. On the other hand, if the name is
longer than 100 letters, you can't store it in that variable. In terms
of ease of use, it's wonderful. If you're worried about memory wastage
or having a name that's over 100 (or whatever you set your limit to),
this method cannot be adopted. You must dynamically allocate memory for
it and point to it.

I'm not sure if you want me to get into the details of that and it
might take a fair bit to explain (many things to cover) so I'm going to
end it here. If you ask for an explanation, I'd be glad to help (as I'm
sure many others would be too).

Cheers,
Sharvil Nanavati

P.S. I'm still furious over that VESA issue! GRR!!

__________________________________________________
Do You Yahoo!?
Yahoo! Messenger - Talk while you surf!  It's FREE.
http://im.yahoo.com/