|
From: Ioannis V. <iv...@em...> - 2003-06-07 10:58:53
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Andr? Macario Barros
> Sent: Friday, June 06, 2003 3:16 PM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] Single Linked List-IV
>=20
>=20
> Dear users,
> Please consider this (modificated) Single Linked List Code:
>=20
>=20
> #include <stdio.h>
> #include <stdlib.h>
>=20
> #define MAX 5
>=20
> struct address
> {
> char name[40];
> char phone[20];
> struct address *next;
> };
>=20
> void slstore(struct address *, struct address **, struct address **);
> void display(struct address *);
> void list(struct address **);
> void init(struct address **);
>=20
> int main()
> {
> struct address *info=3DNULL, *start=3DNULL, *last=3DNULL;
> int i;
>=20
> for(i=3D0; i<MAX; i++)
> {
> init(&info);
> printf("Enter name: ");
> gets(info->name);
Dangerous, you can't expect the user to know the internals of your code =
and
limit himself to 39 characters. Use fgets() instead. Avoid gets() in
general.
fgets(info->name, 40, stdin);
> printf("Entre com phone: ");
> gets(info->phone);
fgets(info->phone, 20, stdin);
> slstore(info, &start, &last);
> }
> printf("List Recorded:\n");
> list(&start);
> while (start)
> {
> info =3D start;
> start =3D start->next;
> free(info);
> }
> system("PAUSE");
> return(0);
> }
>=20
> void slstore(struct address *i, struct address **start_arg,
> struct address **last_arg)
> {
> if(*last_arg=3D=3DNULL)
*start_arg=3Di;
> else
(*last_arg)->next=3Di;
> i->next=3DNULL;
> *last_arg=3Di;
> }
I made the above more readable so as to understand it. Why do you use =
struct
**s and not struct *s and avoid those *s?
I cannot understand the last line, it seems to contradict with the else
statement at least. I suspect a bug here. Ommiting the last line it =
becomes:
void slstore(struct address *i, struct address *start_arg,
struct address *last_arg)
{
if(last_arg=3D=3DNULL)=20
start_arg=3Di;
=20
else
last_arg->next=3Di;
=20
i->next=3DNULL;
}
>=20
> void display(struct address *info)
> {
> printf("Actual address-->%p\n", info);
When you pass a different pointer than expected in a variadic function =
you
must cast:
printf("Actual address-->%p\n", (void *)info);
> printf("%s\n", info->name);
> printf("%s\n", info->phone);
> printf("Next address-->%p\n", info->next);
printf("Next address-->%p\n", (void *) info->next);
> printf("\n");
> system("PAUSE");
> }
>=20
> void list(struct address **start_arg)
> {
> struct address *info;
>=20
> init(&info);
> info =3D *start_arg;
> while(info)
> {
> display(info);
> info =3D info->next;
> }
> }
You use **s while they are not needed in the whole program.
> void init(struct address **arg)
> {
> *arg=3D malloc(1*sizeof(struct address));
> if(!(*arg))
> {
> printf("sem mem=F3ria\n");
> system("PAUSE");
> exit(EXIT_FAILURE);
> }
> }
The same here. Without **s:
void init(struct address *arg)
{
arg=3D malloc(sizeof(*arg));
if(!arg)
{
printf("He was a good man, always to remember him\n");
system("PAUSE");
exit(EXIT_FAILURE);
}
}
> =3D=3D>My questions:
> A) One example of addresses that are printed on the screen are:
> 002224C8
> 00222510
> 00222558
> 002225A0
> 002225E8
> The distance between theese addresses is 72 bytes, not 64=20
> as expected
> because the size of the struct address.
> WHY?
If I recall correctly you create them on the free store, so their =
address
can be anywhere.
>=20
> B) Could the final loop:
> while (start)
> {
> info =3D start;
> start =3D start->next;
> free(info);
> }
> be changed by a function call for a function like
>=20
> free_ptr(&start);
>=20
> void free_ptr(struct address **arg)
> {
> struct address *info=3DNULL;
>=20
> while (*arg)
> {
> info =3D *arg;
> *arg =3D (*arg)->next;
> free(info);
> }
> }
Yes, isn't it exactly the same with the unneeded use of **s? Better:
void free_ptr(struct address *arg)
{
struct address *info;
=20
while (arg)
{
info =3D arg;
arg =3D arg->next;
free(info);
}
}
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|