# Structures and chained lists questions :

L

#### lab top

struct order
{
char Name[CMAX];
char Article[CMAX];
int numb, price ;
struct cde *next ;
} ;
....

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 (i’m not sure if i’m right saying NULL
is zero in memory but that’s not what I’m 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 it’s 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
....

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

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 it’s different from NULL...

/*E*/ If it’s the first order, l_order receives curr from preceding
fscanf

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

/*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 ?

....

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 don’t 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

E

#### Eric Sosman

struct order
{
char Name[CMAX];
char Article[CMAX];
int numb, price ;
struct cde *next ;
} ;
...

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 (i’m not sure if i’m right saying NULL
is zero in memory but that’s not what I’m 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 it’s 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
...

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

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.

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).

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

### Members online

No members online now.