Here is the sample program

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

//  COMPILER    :   GNU GCC COMPILER
//  EDITOR      :   CODE::BLOCKS 8.02

struct test
{
    int data;
    struct test *link;
};

void change(struct test *ptr);

int main()
{
    struct test *fresh = (struct test *)malloc(sizeof(struct test));

    int some_data = 10;

    fresh -> data = some_data;
    fresh -> link = NULL;

    printf("\n\n\tBefore Going Into Function");

    printf("\n\n\tData = %d\t\tAddress %x", fresh -> data, fresh);
    printf("\n\n\t____________________________________________________________");

    change(fresh);

    printf("\n\n\tAfter Getting Out Of Function");

    printf("\n\n\tData = %d\t\tAddress %x", fresh -> data, fresh);
    printf("\n\n\t____________________________________________________________");

    return 0;
}

void change(struct test *some_ptr)
{
    struct test *new_fresh = (struct test *)malloc(sizeof(struct test));

    some_ptr = new_fresh;

    int some_new_data = 20;

    new_fresh -> data = some_new_data;
    new_fresh -> link = NULL;

    printf("\n\n\tInside Function");
    printf("\n\n\tData = %d\t\tAddress %x", some_ptr -> data, some_ptr);
    printf("\n\n\t____________________________________________________________");
}

And this is the output

Before Going Into Function

        Data = 10               Address 3d2488

        ____________________________________________________________

        Inside Function

        Data = 20               Address 3d2508

        ____________________________________________________________

        After Getting Out Of Function

        Data = 10               Address 3d2488

        ____________________________________________________________
Process returned 0 (0x0)   execution time : 0.000 s
Press any key to continue.

Hence, we see that the original structure variable is not changed.
So my question is how do i send the reference of the structure variable so that my output becomes

Before Going Into Function

        Data = 10               Address 3d2488

        ____________________________________________________________

        Inside Function

        Data = 20               Address 3d2508

        ____________________________________________________________

        After Getting Out Of Function

        Data = 20               Address 3d2508

        ____________________________________________________________
Process returned 0 (0x0)   execution time : 0.000 s
Press any key to continue.

I tried the concept of using & in the actual parameter and ** in the formal parameter but it gave errors.

BTW, i can't return the structure variable. It is a requirement of the program. (It's not a homework assignment, I'll use this concept in DEQUE by returning both head and tail of the list)

Thanking in advance

I think this is what you want: - Note your code generates a memory leak on line 43...

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


struct test
{
    int data;
    struct test *link;
};

void change(struct test **ptr);

int main()
{
	struct test *fresh = (struct test *)malloc(sizeof(struct test));

	int some_data = 10;

	fresh -> data = some_data;
	fresh -> link = NULL;

	printf("\n\n\tBefore Going Into Function");

	printf("\n\n\tData = %d\t\tAddress %p", fresh -> data, (void*)fresh);
	printf("\n\n\t____________________________________________________________");

	change(&fresh);

	printf("\n\n\tAfter Getting Out Of Function");

	printf("\n\n\tData = %d\t\tAddress %p", fresh -> data, (void*)fresh);
	printf("\n\n\t____________________________________________________________");

	return 0;
}

void change(struct test **some_ptr)
{
	int some_new_data = 20;

	struct test *new_fresh = (struct test *)malloc(sizeof(struct test));

	*some_ptr = new_fresh;

	/*(*some_ptr) -> data = some_new_data;
	(*some_ptr) -> link = NULL;*/
	new_fresh->data = some_new_data;
	new_fresh->link = NULL;  

	printf("\n\n\tInside Function");
	printf("\n\n\tData = %d\t\tAddress %p", new_fresh -> data, (void*)new_fresh);
	printf("\n\n\t____________________________________________________________");
}

I tried the concept of using & in the actual parameter and ** in the formal parameter but it gave errors.

That's the direction you want to head. Perhaps post that attempt.

BTW, is leaking memory like you are doing also a requirement?

Note your code generates a memory leak on line 43

BTW, is leaking memory like you are doing also a requirement?

Since it was a small program, i thought it wouldn't hurt to have a small leak. Don't worry, I always free memory in big programs and hence forth, I'll also do even in small programs.

Okay, done! Thanks gerard4143!
Now i can go back to my double-link list and DEQUE program.

There is also another clarification. How do I fix the memory leak? I've made an attempt inside the program. Please check whether it's correct or not...

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

//  COMPILER    :   GNU GCC COMPILER
//  EDITOR      :   CODE::BLOCKS 8.02

struct test
{
    int data;
    struct test *link;
};

void change(struct test **);

int main()
{
    struct test *fresh = (struct test *)malloc(sizeof(struct test));
    struct test *temp = NULL;

    int some_data = 10;

    fresh -> data = some_data;
    fresh -> link = NULL;

    printf("\n\n\tBefore Going Into Function");

    printf("\n\n\tData = %d\t\tAddress %x", fresh -> data, fresh);
    printf("\n\n\t____________________________________________________________");

    change(&fresh);

    printf("\n\n\tAfter Getting Out Of Function");

    printf("\n\n\tData = %d\t\tAddress %x", fresh -> data, fresh);
    printf("\n\n\t____________________________________________________________");

    return 0;
}

void change(struct test **some_ptr)
{
    struct test *new_fresh = (struct test *)malloc(sizeof(struct test));

    free(*some_ptr);        //  SHOULD I BE DOING SOMETHING LIKE THIS?
                            //  OR IS THIS REDUNDANT?
                            //  PESONAL OPINION - IT SHOULD BE DONE

    *some_ptr = new_fresh;

    new_fresh -> link = NULL;

    new_fresh -> data = 20;

    printf("\n\n\tInside Function");
    printf("\n\n\tData = %d\t\tAddress %x", new_fresh -> data, new_fresh);
    printf("\n\n\t____________________________________________________________");
}

Okay, done! Thanks gerard4143!
Now i can go back to my double-link list and DEQUE program.

There is also another clarification. How do I fix the memory leak? I've made an attempt inside the program. Please check whether it's correct or not...

void change(struct test **some_ptr)
{
    struct test *new_fresh = (struct test *)malloc(sizeof(struct test));

    free(*some_ptr);        //  SHOULD I BE DOING SOMETHING LIKE THIS?
                            //  OR IS THIS REDUNDANT?
                            //  PESONAL OPINION - IT SHOULD BE DONE

    *some_ptr = new_fresh;

    new_fresh -> link = NULL;

    new_fresh -> data = 20;

    printf("\n\n\tInside Function");
    printf("\n\n\tData = %d\t\tAddress %x", new_fresh -> data, new_fresh);
    printf("\n\n\t____________________________________________________________");
}

Yes this will get ride of the memory leak...but it will also get get of the pointer you passed to the function.
The point I'm trying to make is, is this the functionality you want in this function? Dynamic memory runtime errors are very hard to track down so I would take a minute and think about it...

Yes this will get ride of the memory leak...but it will also get get of the pointer you passed to the function.
The point I'm trying to make is, is this the functionality you want in this function? Dynamic memory runtime errors are very hard to track down so I would take a minute and think about it...

The functionality that I would like is as follows

There is a Double-Link List or Two-Way List.
There is also a function

void add_node_at_front ( int element_to_be added , struct **list head_of_list );

Now if a new element is to be added, head address will also be modified. I could have done it by returning the address. But my special requirement is not to return but instead pass the reference of the head address which will be modified. And hence, this thread.

Now i didn't understand this part

but it will also get get of the pointer you passed to the function

??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.