Function Does Not Change Passed Pointer C++

Function does not change passed pointer C++

Because you are passing a copy of pointer. To change the pointer you need something like this:

void foo(int **ptr) //pointer to pointer
{
*ptr = new int[10]; //just for example, use RAII in a real world
}

or

void bar(int *& ptr) //reference to pointer (a bit confusing look)
{
ptr = new int[10];
}

Passing a pointer to a function doesn't change its value

I think all the people who has answered your question could not understood your small question. You are asking as array in c behave/act like a pointer why your below line of code is not working?

device = &rxBuf[0];//as you assigning the address of first character to
//your `device array` its not going to work as device is just a character array and not array pointer

it should be like below

*device = rxBuf[0];

To copy more than one character you can use a for loop like below.

int i;
for(i=0; i<3; i++){
*(device + i) = rxBuf[i];
}

Calling a function with a pointer does not change value of that pointer? [duplicate]

Changing the value of a function's variable (including those declared as parameters) has no effect on the caller.

void bad1(int i) {
i = 1; // No effect on the caller.
}

void bad2(void* p) {
p = NULL; // No effect on the caller.
}

If you want to change a variable in the caller, you will need to pass a pointer to it.

void good1(int* i_ptr) {
*i_ptr = 1;
}

void good2(void** p_ptr) {
*p_ptr = NULL;
}

So either pass a pointer to a pointer, or pass a pointer to an-already allocated structure. You could use

Animation ani;
Animation_init(&ani, name);
...
Frame* frame = Frame_new(name);
Animation_append_frame(&ani, frame);
...
Animation_destroy(&ani);

or

Animation* ani = Animation_new(name);
...
Frame* frame = Frame_new(name);
Animation_append_frame(ani, frame);
...
Animation_delete(ani);

assuming you had

typedef struct Frame {
char* name;
struct Frame* next;
} Frame;

typedef struct {
char* name;
Frame* first_frame;
Frame* last_frame;
} Animation;

void Animation_init(Animation* self, const char* name) {
self->name = strdup(name);
self->first_frame = NULL;
self->last_frame = NULL;
}

void Animation_destroy(Animation* self) {
Frame* head = self->first_frame;
while (head != NULL) {
Frame* next = head->next;
Frame_delete(head);
head = next;
}

free(self->name);
}

Animation* Animation_new(const char* name) {
Animation* self = malloc(sizeof(Animation));
Animation_init(self, name);
return self;
}

void Animation_delete(Animation* self) {
Animation_destroy(self);
free(self);
}

void Animation_append_frame(Animation* self, Frame* frame) {
if (self->last_frame == NULL) {
self->first_frame = frame;
} else {
self->last_frame->next = frame;
}

while (frame->next != NULL)
frame = frame->next;

self->last_frame = frame;
}

Change passed pointer in function

In a function, changes to pointer variables or pointer parameters have no effect outside the function. However, if the pointer is pointing to an object outside the function, that object can be modified by dereferencing the pointer.

For example, in OP's test function:

void test(int **nPptr, int *n2Ptr) {
int *p = *nPptr;

p = n2Ptr;
}

p is initialized and then its value is changed by an assignment. This has no effect on any object outside the function. If the function were changed as follows:

void test(int **nPptr, int *n2Ptr) {
int *p = *nPptr;

p = n2Ptr;

*nPptr = p;
*p = 42;
}

Then two objects outside the function will have been modified (an int * and an int).

In OP's remove_by_index function, changes to the current variable as it progresses through the linked list have no external effect, but the line:

current->next = temp_node->next;

is equivalent to:

(*current).next = (*temp_node).next;

The external Person object that current is pointing to on the linked list has been modified by dereferencing of the pointer and assignment to the next member of the Person it is pointing to.

How do I modify a pointer that has been passed into a function in C?

You need to pass in a pointer to a pointer if you want to do this.

void barPush(BarList ** list,Bar * bar)
{
if (list == NULL) return; // need to pass in the pointer to your pointer to your list.

// if there is no move to add, then we are done
if (bar == NULL) return;

// allocate space for the new node
BarList * newNode = malloc(sizeof(BarList));

// assign the right values
newNode->val = bar;
newNode->nextBar = *list;

// and set the contents of the pointer to the pointer to the head of the list
// (ie: the pointer the the head of the list) to the new node.
*list = newNode;
}

Then use it like this:

BarList * l;

l = EMPTY_LIST;
barPush(&l,&b1); // b1 and b2 are just Bar's
barPush(&l,&b2);

Jonathan Leffler suggested returning the new head of the list in the comments:

BarList *barPush(BarList *list,Bar *bar)
{
// if there is no move to add, then we are done - return unmodified list.
if (bar == NULL) return list;

// allocate space for the new node
BarList * newNode = malloc(sizeof(BarList));

// assign the right values
newNode->val = bar;
newNode->nextBar = list;

// return the new head of the list.
return newNode;
}

Usage becomes:

BarList * l;

l = EMPTY_LIST;
l = barPush(l,&b1); // b1 and b2 are just Bar's
l = barPush(l,&b2);

Why is the pointer declared in main() not changed? [duplicate]

And also call by reference, in both of these methods the changes made in the function are reflected in actual arguments' variable.

There is an important difference, though: the changes are always made to whatever is referenced/pointed to, never to the reference/pointer itself (modifying a reference is impossible in general).

That is why assigning c a new value inside foo has no effect on c outside foo: the pointer passed to a function is copied.

If you need to modify the pointer, you need to add another level of dereference by passing a pointer reference or a pointer to a pointer.

What is wrong in passing pointer variable as an function argument instead of address of variable in C?

The function parameter p

void fun(int *p)
{
p = &b;
}

is a local variable of the function. The function deals with a copy of the value of the pointer passed to the function

fun(p);

So within the function it is its local variable that is changed.

You may imagine the function and its call the following way (I will rename the function parameter to ptr for clarity)

fun(p);

//...

void fun( /* int *ptr */ )
{
int *ptr = p;
ptr = &b;
}

As you can see the original pointer p was not changed.

If you want to change the pointer you need to pass it by reference. In this case the function will look like

void fun( int **p)
{
*p = &b;
}

and in main it is called like

fun( &p );

In this case the pointer p (and not a copy of its value) will be indeed changed in the function.

That is the general rule is if you want to change an object in a function you nned to pass it to the function by reference. In C passing by reference means passing an object indirectly through a pointer to it. In this case dereferencing the pointer you will get a direct access to the original object.

pointer not updating the value it is pointing to inside void function

a is a copy of the pointer that was passed. At the end of update, a is lost. When you do this:

a = &temp3;

You change the value of a, but that doesn't matter, because a is gone after that anyway. Instead, assign the value to where it's pointing at, much like you did with b:

*a = temp3;

You could also use references instead of pointers:

void update(int &a, int &b) {
int temp = a;
int temp2 = b;
int temp3 = temp + temp2;
printf("%d ", temp3);
b = abs(a - b);
a = temp3;
}

int main() {
int a, b;
scanf("%d %d", &a, &b);
update(a, b);
printf("%d\n%d", a, b);
return 0;
}

Unable to modify pointer variable passed as argument to a function

Consider this:

void foo (int j)
{
j = 7;
}

foo (8);

What do you expect to happen here? A function can't change a value in the caller this way.

What should happen here:

rt_exist (NULL);

For C++, you can use references:

int rt_exist(struct route_entry*& prev_rte) {

prev_rte = rte_head; //This doen't assigns rte_head to prev_rte

return 0;

}

For C, you need to pass a pointer to the thing you want to change:

int rt_exist(struct route_entry** prev_rte_ptr) {

*prev_rte_ptr = rte_head; //This doen't assigns rte_head to prev_rte

return 0;

}


Related Topics



Leave a reply



Submit