Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Structures and chained lists questions :

Reply
Thread Tools

Structures and chained lists questions :

 
 
lab top
Guest
Posts: n/a
 
      02-12-2011

struct order
{
char Name[CMAX];
char Article[CMAX];
int numb, price ;
struct cde *next ;
} ;
....
1
struct order *l_order = NULL ; /*A ... order list */
struct order *prec, *curr ; /* B... precedent and current order */
....
/*A*/ I understand that l_order is a pointer to a structure of type
order (meant to receive data of type order) and it is located at
adress zero or NULL in memory (im not sure if im right saying NULL
is zero in memory but thats not what Im trying to understand).

/*B*/ Here I understand that prec and curr are pointers to a structure
of type order however here their adress in memory is not defined. I
get confused with this declaration because later I assume prec and
curr already point to an order... on line B its not the case, they
are just pointer to a type of data and nothing else...?

....
following 2 and 3 are in a do while (ret_val != EOF) loop
....
2
if ((curr = malloc(sizeof(struct order))) == NULL)
{
printf("\nOut of memory\n") ;
exit(EXIT_FAILURE) ;
}

I understand that the curr pointer of type structure to an order
receives memory for one entry

3
ret_val = fscanf(FP_SOURCE, "%s %s %d %d", curr->Nom, curr->Art,
&(curr->nbr), &(curr->prix)) ;

if (ret_val == EOF)
{
free(curr) ; /*C*/
if (l_order != NULL) /*D*/
prec->next = NULL ;
}
else
{
if (l_order == NULL) /*E*/
l_order = curr ;
else /*F*/
prec->next = curr ;
prec = curr ; /*G*/
}

/*C*/ fscanf has reached the end of the file and curr is positioned at
an adress that is not necessary anymore, is it the reason why curr is
freed ?

/*D*/ I understand that line C tells that if l_order is not at address
zero in memory as it is declared on line A, it means that at least one
order has been entered...it then sets a pointer to the next order to
NULL so it can later display orders until its different from NULL...

/*E*/ If its the first order, l_order receives curr from preceding
fscanf

/*F*/ If its not the first order, the next adress of prec receives
the current order. Is this meant for backup because line G receives
the same adress ?

/*G*/ prec receives the current data read from fscanf ... so at this
point prec and prec->suiv are located at the same adress in memory ?

....
4
for (curr = l_order ; curr != NULL ; curr = curr->next)
print_order(*curr) ;

This is to print the list of order from the file... it somehow follows
the code from line G... without changes of variables or pointers.

However, my big question about structures and chained list is that in
this for loop curr points at the adress of the first order in the
list, it prints it and it then goes to the next location in the order
list see 3

Here I would have used prec = prec->next since in /* D, F and G */
prec is used to move into memory location following what it gets from
curr and fscanf...

To make it simple, I dont see how curr can move in the memory
location of the order list. At least I would have expected to see
curr = prec somewhere before
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      02-12-2011
On 2/12/2011 9:55 AM, lab top wrote:
>
> struct order
> {
> char Name[CMAX];
> char Article[CMAX];
> int numb, price ;
> struct cde *next ;
> } ;
> ...
> 1
> struct order *l_order = NULL ; /*A ... order list */
> struct order *prec, *curr ; /* B... precedent and current order */
> ...
> /*A*/ I understand that l_order is a pointer to a structure of type
> order (meant to receive data of type order) and it is located at
> adress zero or NULL in memory (im not sure if im right saying NULL
> is zero in memory but thats not what Im trying to understand).


`l_order' is the name of a variable. Some variables hold `int'
values, some hold `double' values, this one holds `pointer-to-struct-
order' values. A variable can be given an initial value (if you like),
a value it already holds before you've done anything else with it.
An `int' value can be initialized to hold 1 or 42 or -666, a `double'
value can be initialized with 2.14159 or -3.7828, and so on. A
`pointer-to-struct-order' variable can be initialized with the value
that points to an actual `struct order' somewhere, or it can be given
the special value `NULL', which means "I'm not pointing at anything."
Despite its apparent vacuousness, this is a useful state of affairs:
If you have a variable that points to the President of Egypt, it is
worth being able to say "Nobody's home."

The "zero" thing is a red herring. If you look under the covers,
you'll find that NULL is "spelled" as `0' (possibly with decorations),
but that's just the source-code way of saying "Nobody's home." In C
source code you may write `1.414' to designate a value, but the `.'\
does not appear in the actual value that is designated; it's purely a
source-code construct. Similarly with the `0' that underlies NULL:
it's just a source-code construct, a way of saying "Produce the pointer
value that means Nobody's home, whatever that may be."

> /*B*/ Here I understand that prec and curr are pointers to a structure
> of type order however here their adress in memory is not defined. I
> get confused with this declaration because later I assume prec and
> curr already point to an order... on line B its not the case, they
> are just pointer to a type of data and nothing else...?


`prec' and `curr' are also variable names, and are also names of
variables that can hold `pointer-to-struct-order' values. They have
not been given any values yet, just as `int what;' and `double who;'
have not been given values. (Actually, this depends on whether the
variables are declared inside or outside function blocks -- but that's
a side-issue I'd encourage you to ignore for the moment.)

What values to `prec' and `curr' and `who' and `what' hold? No
proper "values" at all; their values are "indeterminate." If you try
to print the value of `what' or add forty-seven to `who' you will get
unpredictable results; if you try to use the value of `prec' or `curr'
you will get unpredictable results.

> ...
> following 2 and 3 are in a do while (ret_val != EOF) loop
> ...
> 2
> if ((curr = malloc(sizeof(struct order))) == NULL)
> {
> printf("\nOut of memory\n") ;
> exit(EXIT_FAILURE) ;
> }
>
> I understand that the curr pointer of type structure to an order
> receives memory for one entry


The malloc() call reserves enough memory for a `struct order',
and returns a value pointing to that memory. The assignment stores
that pointer value in `curr', which thereby loses its indeterminacy
and becomes usable.

> 3
> ret_val = fscanf(FP_SOURCE, "%s %s %d %d", curr->Nom, curr->Art,
> &(curr->nbr),&(curr->prix)) ;


... and here you use the value you earlier stored in `curr'.

> if (ret_val == EOF)
> {
> free(curr) ; /*C*/
> if (l_order != NULL) /*D*/
> prec->next = NULL ;


This is bad. `prec' was not initialized, so it began life with
an indeterminate value. Nothing has been stored in it, so its value
is still indeterminate. When you try to make use of that "garbage"
value, something unpredictable -- but probably unwelcome -- will happen.

I'd like to help a bit more, but I've got other errands to attend
to right now. Others will probably chime in, and I'd also recommend
that you look at the FAQ <http://www.c-faq.com/>, where there's some
good material on pointers (and on NULL).

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chained RDP over chained VPN Frank Winkler Cisco 2 06-20-2008 08:02 AM
Chained call pattern with inheritance, polymorphism and generics... Daniel Pitts Java 19 09-30-2007 05:24 PM
List of lists of lists of lists... =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==?= Python 5 05-15-2006 11:47 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
NAT and chained subnet bomba Cisco 6 10-29-2003 02:09 PM



Advertisments