pointer to structure passed by ref 
Author Message
 pointer to structure passed by ref

consider the following code

/***********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student {
        int id;
        char name [100];
        struct student * next;

Quote:
};

void printStudents(struct student *p) {
        printf("Printing the list of students \n");
        for (p; p != NULL; p=p->next)
        printf ("id=%d name=%s \n",p->id,p->name);

        printf("\n");

Quote:
}

int main () {
                struct student *p, *head, *mid, *tail;

                p=(struct student *) malloc (sizeof(struct student));
                p->id=12;    strcpy(p->name,"Tom");             p->next=NULL;        head=p;  
        tail=p;

        p=(struct student *) malloc (sizeof(struct student));
                p->id=7;     strcpy(p->name,"Bob");             p->next=NULL;        tail->
next=p;         tail=p;                 mid=p;

                p=(struct student *) malloc (sizeof(struct student));
                p->id=9;     strcpy(p->name,"Rob");             p->next=NULL;        
        tail-> next=p;       tail=p;

                printStudents(head);

                printStudents(head);

                return 0;

Quote:
}

/****************************************************************************/

My question is head is a pointer to student struct and is passed by
ref to function printStudents when I call this function for the second
time it give me same result why?

Is there any that I can change head in printStudent and that change
also effects in main function.

thank you.



Sat, 10 Dec 2005 15:35:28 GMT  
 pointer to structure passed by ref

Quote:
> consider the following code
> /***********************************************************************/
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> struct student {
>         int id;
>         char name [100];
>         struct student * next;
> };
> void printStudents(struct student *p) {
>         printf("Printing the list of students \n");
>         for (p; p != NULL; p=p->next)
>    printf ("id=%d name=%s \n",p->id,p->name);
>         printf("\n");
> }
> int main () {
>            struct student *p, *head, *mid, *tail;
>            p=(struct student *) malloc (sizeof(struct student));
>            p->id=12;    strcpy(p->name,"Tom");             p->next=NULL;        head=p;  
>            tail=p;
>            p=(struct student *) malloc (sizeof(struct student));
>            p->id=7;     strcpy(p->name,"Bob");             p->next=NULL;        tail->
> next=p;    tail=p;                 mid=p;
>            p=(struct student *) malloc (sizeof(struct student));
>            p->id=9;     strcpy(p->name,"Rob");             p->next=NULL;        
>    tail-> next=p;       tail=p;
>            printStudents(head);
>            printStudents(head);
>            return 0;
> }
> /****************************************************************************/
> My question is head is a pointer to student struct and is passed by
> ref to function printStudents when I call this function for the second
> time it give me same result why?

Because the function printStudents() can only modify its local copy of
the parameter "p". You are not passing by reference, you're passing a
pointer by address. Just because you're passing a pointer does not
magically mean passing by value means passing by reference.

Quote:
> Is there any that I can change head in printStudent and that change
> also effects in main function.

Pass the *address* of head and make printStudent() accept a
struct student ** instead of a struct student *.

You would also want to see the comp.lang.c FAQ. This question has been
asked here thousands of times. The fact that pointers are passed by
value just like anything else might seem confusing at first - after all,
pointers are used to fake passing by reference - but it is logical when
you come to think about it.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"This is a personnel commuter."
   - Train driver in Scientific American



Sat, 10 Dec 2005 15:43:32 GMT  
 pointer to structure passed by ref
Thanx for your answer. I am afraid that I have not much time to take
look at comp.c faq. one more quicke question. I have a function.

/***************************************************************************
        Implement this function so that your data structures are cleaned
        and associated memory freed up.

*/
#include "ern_expression.h"

void finalize (exp_ptr head, exp_ptr tail) {
#ifdef TRACE
        printf ("TRACE: in function finalize \n");
#endif

Quote:
}

/****************************************************************/

here exp_ptr is
typedef struct ern_exp * exp_ptr;

typedef struct ern_exp {
  char token[MAXLENGTH] /*MAXLENGTH = 30*/
  int value;
  exp_ptr next;

Quote:
}ern_node;

which defined in the header file that is included above

I have to pass the head and tail so that I clean up the memory using
free(p);
is there anywhay that I can't change the param and do that action.



Sun, 11 Dec 2005 00:20:40 GMT  
 pointer to structure passed by ref

Quote:
> Thanx for your answer. I am afraid that I have not much time to take
> look at comp.c faq. one more quicke question. I have a function.
> /***************************************************************************
>    Implement this function so that your data structures are cleaned
>    and associated memory freed up.
> */
> #include "ern_expression.h"
> void finalize (exp_ptr head, exp_ptr tail) {
> #ifdef TRACE
>    printf ("TRACE: in function finalize \n");
> #endif
> }
> /****************************************************************/
> here exp_ptr is
> typedef struct ern_exp * exp_ptr;
> typedef struct ern_exp {
>   char token[MAXLENGTH] /*MAXLENGTH = 30*/
>   int value;
>   exp_ptr next;
> }ern_node;
> which defined in the header file that is included above
> I have to pass the head and tail so that I clean up the memory using
> free(p);
> is there anywhay that I can't change the param and do that action.

I'm afraid I don't understand your question. I'll make do with a best
guess.
If you are asking how you can free your list without being able to
change the variable in the original function (for example main()), the
answer is that you don't *need* to change the variable in the original
function. Just use free() in a loop freeing each node until you reach
the tail. The memory will still be freed. The original variable will
be left pointing to non-allocated memory, but if you don't use that
variable, all will be fine.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"You could take his life and..."
   - Mirja Tolsa



Sun, 11 Dec 2005 00:59:52 GMT  
 pointer to structure passed by ref
Ok thank you for reply.

You mean that the values from head to tail still exist but they are
freed. If yes then how should I trace memory leaks in my program.



Sun, 11 Dec 2005 04:57:05 GMT  
 pointer to structure passed by ref


If the following is not responsive to your question, could you explain
better what you want?  Note the new function 'modify'.  BTW, call-by-
reference has no meaning in C; all parameters are passed by value.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct student
{
        int id;
        char name[100];
        struct student *next;

Quote:
};

void printStudents(struct student *p)
{
        printf("Printing the list of students \n");
        for (; p; p = p->next)
                printf("id=%d name=%s \n", p->id, p->name);
        printf("\n");

Quote:
}

void modify(struct student *p, int id, char *name)
{
        p->id = id;
        strcpy(p->name, name);

Quote:
}

int main()
{
        struct student *p, *head, *mid, *tail;

        p = malloc(sizeof *p);
        if (p)
                *p = (struct student) {
                .id = 12,.name = "Tom",.next = NULL};
        else {
                fprintf(stderr, "malloc failure\n");
                exit(EXIT_FAILURE);
        }
        head = p;
        tail = p;

        p = malloc(sizeof *p);
        if (p)
                *p = (struct student) {
                .id = 7,.name = "Bob",.next = NULL};
        else {
                fprintf(stderr, "malloc failure\n");
                exit(EXIT_FAILURE);
        }
        tail->next = p;
        tail = p;
        mid = p;

        p = malloc(sizeof *p);
        if (p)
                *p = (struct student) {
                .id = 9,.name = "Rob",.next = NULL};
        else {
                fprintf(stderr, "malloc failure\n");
                exit(EXIT_FAILURE);
        }
        tail->next = p;
        tail = p;

        printStudents(head);
        modify(head, 17, "Becky");
        printStudents(head);
        for (p = head; p; p = tail) {
                tail = p->next;
                free(p);
        }

        return 0;

Quote:
}

--
Martin Ambuhl
now exiled to
Hurricane Bait, Texas


Sun, 11 Dec 2005 08:03:27 GMT  
 pointer to structure passed by ref


Quote:
> Thanx for your answer. I am afraid that I have not much time to take
> look at comp.c faq. one more quicke question. I have a function.

If you don't have time to check the FAQ, we don't have the time to bother
with you.

*plonk*

--
Martin Ambuhl
now exiled to
Hurricane Bait, Texas



Sun, 11 Dec 2005 08:04:58 GMT  
 pointer to structure passed by ref

Quote:
> Ok thank you for reply.
> You mean that the values from head to tail still exist but they are
> freed. If yes then how should I trace memory leaks in my program.

To properly trace memory leaks you need a de{*filter*}, which is outside the
scope of comp.lang.c. Within the C language itself you will have to do
with checking that each malloc() call is matched with a free() call.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.*-*-*.com/ ~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"You will be given the plague."
   - Montgomery Burns



Sun, 11 Dec 2005 16:22:17 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. Cannot pass x as ref or ref out...

2. Simple Question: Passing Interface Pointers, and Ref counts

3. newbie pointer to structure passing.

4. Passing structure pointers as arguments

5. Passing a structure pointer

6. updating structure elements from a pointer passed to the function

7. Passing pointers to elements of a structure.

8. Passing structure pointers to functions?

9. Passing pointers at structures

10. Passing pointer to structure

11. Passing a pointer to array of structures - How?

12. Structure pointers, char, and passing

 

 
Powered by phpBB® Forum Software