L
lab top
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 (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
....
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 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
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 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
{
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 (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
....
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 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
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 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