|
From: Per W. <pw...@ia...> - 2003-06-07 19:43:00
|
The slstore() function DOES need it's last line, or else the list will not
get a pointer to the last element. The pointer to the last element is used
to allow insert of new elements last, without having to iterate throught
the list.
I know it's a bit extra noisy with dual indirection, but since the code
is C and not C++, slstore() can't take references to pointers.
/Per W
On Sat, 7 Jun 2003, Ioannis Vranos wrote:
> > -----Original Message-----
> > From: dev...@li...
> > [mailto:dev...@li...] On Behalf
> > Of Andr? Macario Barros
> > Sent: Friday, June 06, 2003 3:16 PM
> > To: Dev-Cpp-Users
> > Subject: [Dev-C++] Single Linked List-IV
> >
> >
> > Dear users,
> > Please consider this (modificated) Single Linked List Code:
> >
> >
> > #include <stdio.h>
> > #include <stdlib.h>
> >
> > #define MAX 5
> >
> > struct address
> > {
> > char name[40];
> > char phone[20];
> > struct address *next;
> > };
> >
> > void slstore(struct address *, struct address **, struct address **);
> > void display(struct address *);
> > void list(struct address **);
> > void init(struct address **);
> >
> > int main()
> > {
> > struct address *info=3DNULL, *start=3DNULL, *last=3DNULL;
> > int i;
> >
> > 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 a=
nd
> 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);
> > }
> >
> > 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 str=
uct
> **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 becom=
es:
>
> 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;
> }
>
>
>
> >
> > void display(struct address *info)
> > {
> > printf("Actual address-->%p\n", info);
>
> When you pass a different pointer than expected in a variadic function yo=
u
> 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");
> > }
> >
> > void list(struct address **start_arg)
> > {
> > struct address *info;
> >
> > 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
> > 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.
>
>
>
>
>
> >
> > 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
> >
> > free_ptr(&start);
> >
> > void free_ptr(struct address **arg)
> > {
> > struct address *info=3DNULL;
> >
> > 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;
>
> while (arg)
> {
> info =3D arg;
> arg =3D arg->next;
> free(info);
> }
> }
>
>
>
>
>
>
> Ioannis Vranos
>
> * Programming pages: http://www.noicys.freeurl.com
> * Alternative URL 1: http://run.to/noicys
> * Alternative URL 2: http://www.noicys.cjb.net
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The best
> thread debugger on the planet. Designed with thread debugging features
> you've never dreamed of, try TotalView 6 free at www.etnus.com.
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|