append on c (pointers of pointers) - c

I just started learning C language, and I can't fully understand why we should use pointers of pointers in order to append an element into the table (*tab).
here's the code :
#include "append.h"
int append(int ** tab, size_t *size, int value){
int *nouveauTab = realloc(*tab, (*size + 1) * sizeof(int));
if (nouveauTab == NULL){
return 0;
}
*tab = nouveauTab;
(*tab)[*size] = value;
(*size)++;
return 1;
}

If not to use pointer to pointer then the pointer tab will be passed by value. That is the function will deal with a copy of the value of the original pointer. Changing the copy does not influence on the original pointer. It will stay unchanged because it is its copy that was changed within the function.
So you need to pass the pointer by reference.
In C passing by reference means passing an object (a pointer is an object) indirectly through a pointer to it. Thus dereferencing the pointer the function will have a direct access to the original object. In the case of your function to the original pointer like
*tab = nouveauTab;

The short answer is: Because you want to change the value of a pointer in the caller of append. That happens here: *tab = nouveauTab;
The code calling your append function will look something like:
int* table = NULL;
size_t table_size = 0;
if (append(&table, &table_size, 42) == 0)
{
puts("append failed");
}
else
{
printf(size is now %zu and element %zu is %d", table_size, table_size-1, table[table_size-1]);
}
So your code expects that append (on success) will change the value of both table and table_size. That requires that you pass the function pointers to these two variables.
If you did it without usinb pointers, i.e. like:
if (append(table, table_size, 42) == 0)
the function would not be able to change their values as C would just pass a copy of their current values.
A simple example using int
void foo(int x)
{
x = x + 1;
printf("%d\n", x);
}
int x = 42;
printf("%d\n", x);
foo(x);
printf("%d\n", x);
will print
42
43
42 <--- x not changed by `foo` because we passed the value of x (i.e. 42)
but with this:
void foo(int* x)
{
*x = *x + 1;
printf("%d\n", *x);
}
int x = 42;
printf("%d\n", x);
foo(&x);
printf("%d\n", x);
it will print
42
43
43 <--- x was changed by `foo` because we passed a pointer

Related

Understanding pointers & memory address

#include<stdio.h>
int g(int *a, int *b);
int main()
{
int a = 2;
int b = 7;
b = g(&b , &a);
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
int g(int *a, int *b)
{
(*a) = (*a) + 3;
(*b) = 2*(*a) - (*b)+5;
printf("a = %d, b = %d\n", *a, *b);
return (*a)+(*b);
}
The output is:
a = 10, b = 23
a = 23
b = 33
I'm in an Intro to C programming class and having trouble understanding how this works.
Thanks for the help!
Sequencing the events as presented in question:
int main()
{
Declaration of a and b and value assignment:
int a = 2;
int b = 7;
Here is a trick, the address passed to the parameter int* a is actually of b, and vice-versa on the second parameter:
b = g(&b , &a);
Here just printing values of a and b:
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
Since the parameters are pointers, the changes made, in the scope of this function, to the variable addresses pointed by them are permanent:
int g(int *a, int *b) {
Here, dereferencing the pointer (*a, the parentheses are not needed in these cases), means you are now working with the value stored in the address pointed by a, so 7 + 3 = 10, now the value stored in the address pointed by a is = 10:
(*a) = (*a) + 3;
Here, the same thing, dereferencing pointers, so 2 * 10 - 2 + 5 = 23, the value stored in the address pointed by b will be 23:
(*b) = 2*(*a) - (*b)+5;
Here printing a = 10 and b = 23, again, dereferencing pointers means you are working with the values stored in the addresses pointed by them:
printf("a = %d, b = %d\n", *a, *b);
The returned value is 10 + 23 = 33, so for b = g(&b, &a), b will be assigned the value of 33, a is already 23 so it stays that way:
return (*a)+(*b);
}
Remember that C passes all function arguments by value - that means that the formal parameter in the function body is a separate object in memory from the actual parameter in the function call, and the value of the actual parameter is copied to the formal parameter.
For any function to modify the value of a parameter, you must pass a pointer to that parameter:
void foo( T *ptr ) // for any type T
{
*ptr = new_T_value(); // write a new value to the thing ptr points to
}
void bar( void )
{
T var;
foo( &var ); // write a new value to var
}
In the code above, all of the following are true:
ptr == &var
*ptr == var
Thus, when you write a new value to the expression *ptr, it's the same as writing a new value to var.
I think part of what's making this confusing for you is that the names of your formal parameters (a and b) and your pointers (a and b) are flipped - g:a points to main:b and vice versa.
g:a == &main:b // I'm using g: and main: here strictly to disambiguate
*g:a == main:b // which a and b I'm talking about - this is not based on
// any real C syntax.
g:b == &main:a
*g:b == main:a
With & you give the address of the variable to the function, instead of the value.
With * you can access the value of an address.
With b = g(&b , &a); you give the address of the variable b and a to the function.
But you can access the address of b with * a because you declare the function that way: int g (int * a, int * b):
*a points to the address of your b variable.
*b points to the address of your a variable.
I think the different variable names are what confuses you.
To make it easier for yourself you could change the declaration to int g (int * b, int * a)
In case you want to change it:
*b would point to the address of your b variable and
*a would point to the address of your a variable.
by using the * you access the object referenced by the pointer. As the pointers are referencing int variables a & b you do the operations on those variables. I think the same variable names are confusing you
int g(int *p1, int *p2)
{
(*p1) = (*p1) + 3;
(*p2) = 2*(*p1) - (*p2)+5;
printf("*p1 = %d, *p2 = %d\n", *p1, *p2);
return (*p1)+(*p2);
}

Assigning address of pointer to pointer-to-pointer

I am trying to make a simple piece of c code using pointers work but memory is being overwritten unexpectedly.
I want to create an array of pointers to integers then create a pointer to an integer and assign it's address to an array.
Therefore, the array will point to a pointer to an integer. I did this in a function called add_value_to_array().
Here is my code :
void add_value_to_array(int *** array, int * value) {
*array = &value;
}
int main() {
int ** arr = { 0 };
int value = 5;
int * value_ptr = &value;
add_value_to_array(&arr, value_ptr);
//arr = &value_ptr;
printf("looool\n");
printf("looool\n");
printf("looool\n");
printf("looool\n");
printf("%p\n", arr[0]);
}
What I want is :
arr -> value_ptr -> 5
(arr = &value_ptr
*array = value_ptr
value_ptr = &value
*value_ptr = 5
**arr = 5)
however, this is what I have right after add_value_to_array() is called, but memory is overwritten when I call the printfs(), I get garbage values in my arr variable.
Even weirder, if I do directly arr = &value_ptr instead of calling add_value_to_array, things go as expected and memory is not overwritten by the printfs().
So it seems that memory is allocated differently if I use a function or if I do I do things outside of it.
What is happening that I am seeing this behavior?
Following assumption: You want to create an array of length 1 (maybe greater length later) of pointers to int. And you want that single pointer in the array to point to the local variable named 'value'.
Then:
int* arr[] = { 0 }; // or { NULL }, if you prefer
// ^ creates an array; either you specify size explicitly or it will be deduced
// from initializer, as in this case
Arrays automatically decay to pointer to first element, if you pass them to a function. So:
add_value_to_array(arr, &value);
// ^ decays to pointer
// ^ here you still need a pointer; you can create it directly, though
Little problem left: arr decaying to pointer is of type int**. You need the same type in your function:
void add_value_to_array(int** array, int* value)
// ^ one asterisk less
{
*array
// ^ this one is NOW correct: array points to the first element of arr
// dereferencing gives you exactly this element, i. e. the pointer
= /* & */value;
// ^ must be dropped: you already have a pointer and you assign it to
// another one, which is fine.
}
Be aware that pointers simply are addresses of variables somewhere in memory. A bit simplified:
int n = 10;
int* p0 = &n;
int* p1 = &n; // p1 and p0 point both to the same variable n
int* p2 = p0; // now again p2 points to n
*p0 = 12; // change the value pointed to
printf("%d\n", *p2); // will print 12, as both pointers point to the same address.
Function parameters do not differ in this respect, they are just ordinary variables. And it doesn't play a role if the pointee is a data value or a pointer itself:
int n = 10;
int m = 12;
int* p = &n;
int** p1 = &p; // pointer to pointer to int
int** p2 = p1; // both p1 and p2 point to p
*p1 = &m; // re-assign the POINTER
printf("%d\n", **p2); // again prints 12:
// p1 and p2 both point to p which was reset to m, though...
Thank you all for your answers, this helped me find the bug :
I had to pass my value_ptr by address and not by value.
Here is my corrected code :
void add_value_to_array(int *** array, int ** value_ptr) {
*array = value_ptr;
}
int main() {
int ** arr = { 0 };
int value = 5;
int * value_ptr = &value;
add_value_to_array(&arr, &value_ptr);
//arr = &value_ptr;
printf("looool\n");
printf("looool\n");
printf("looool\n");
printf("looool\n");
printf("%p\n", arr[0]);
}
Thank you all for your help !

C Pointers - Structs that are arrays

I have been working on this code for a few hours now and am confused as to why printf is only printing garbage, I am brand new to stack overflow and fairly new to C so please forgive me for any mistakes in this post. I researched for pointers to structs that are arrays and couldn't find anything helpful.
typedef struct my
{
int x;
int y;
} My;
My * main2(void);
void show(void)
{
My * m = main2();
printf("%u\n", m);
printf("%u\n", m);
printf("%d\n", m->x);
printf("%d\n", m->y);
m++;
printf("%u\n", m);
printf("%d\n", m->x);
printf("%d\n", m->y);
m++;
printf("%u\n", m);
printf("%d\n", m->x);
printf("%d\n", m->y);
}
My * main2(void)
{
My j[3];
j[0].x = 2;
j[0].y = 4;
j[1].x = 3;
j[1].y = 5;
j[2].x = 7;
j[2].y = 9;
printf("%u\n", j);
return j;
}
int main()
{
show();
return 0;
}
Variables defined inside a function only have the life-time of that function. Once the function returns, the variables in essence cease to exist.
Now, if you return a pointer to such a variable (or to the first element of an array like you do) and that data no longer exists, then you get undefined behavior when you try to use the pointer.
One way to solve a problem like this is to pass the array (or, again, a pointer to its first element) as an argument to the function:
void main2(My *j)
{
j[0].x = 2;
// And so on...
}
And to pass an array to the function, remember that arrays decays to pointers to their first element when used in a context where a pointer is expected.
That means you could pass it like just about any other variable:
My arr[3];
main2(arr); // equal to main2(&arr[0]);
On another note, the format to print a pointer using printf is "%p". The pointer need to be casted void * as well:
printf("%p\n", (void *) j);
why printf is only printing garbage ? It's because of you are returning the address of a local variable here
My j[3];
...
...
return j; /* j is a local array, its scope is within this this function not outside */
your compiler could have warn you like this
function returns address of local variable [-Werror=return-local-addr]
To overcome this, you can create dynamic array & return it.
Also while printing m which structure pointer, use %p instead of %u format specifier. for e.g
printf("%p\n", (void*)m);
Alright I got the answer, I forgot about this really really important everything in the function is "gone" after the function has returned, thank you to both of the people that helped answer this :)
typedef struct my
{
int x;
int y;
} My;
void getData(My * pos)
{
printf("%d", pos->x);
printf("%d", pos->y);
pos++;
printf("%d", pos->x);
printf("%d", pos->y);
}
int main()
{
My x[2];
x[0].x = 3;
x[0].y = 4;
x[1].x = 5;
x[1].y = 6;
getData(x);
return 0;
}

Pointer as a parameter in C

Let's say a have a pointer as a parameter, why doesn't it's value remain modified after the and of a function, and i have to use this syntax :
void function_name (int **p)
{
// code
}
and in main() :
int *v;
function name (&v);
I want to specify that i use a pointer to a struct type as a parameter.
C passes arguments by value. If you want to modify something in a function and make the modification take effect in the calling function, a pointer to the variable in the calling function has to be passed. Otherwise, any changes made to the variable in a function are only local changes and does not affect the value of the variable in the calling function.
Let's start with an int type variable.
void foo(int x)
{
x = 10;
}
int main()
{
int a = 100;
foo(a); // Value of a does not change in this function
}
In the above program, the value of a remains 100 in main. The line
x = 10;
in foo only affects the value of the variable in foo. To make the change in foo affect the value in main, you'll need to pass a pointer to a.
void foo(int* x)
{
*x = 10;
}
int main()
{
int a = 100;
foo(&a); // Value of a changes in this function
}
Take that analogy to a pointer.
void bar(int* x)
{
x = malloc(10*sizeof(int));
}
int main()
{
int* ptr = NULL;
bar(ptr); // Value of ptr does not change in this function
}
bar allocates memory for an array of 10 ints and assigns the memory to x but that change is local. main does not see it. In main, ptr is still NULL. To make the change in bar affect ptr, a pointer to ptr has to be passed to bar.
void bar(int** x)
{
*x = malloc(10*sizeof(int));
}
int main()
{
int* ptr = NULL;
bar(&ptr); // Value of ptr changes in this function
}
In C, arguments are passed by value. This means that when you pass an argument to a function, a copy of that variable is made. For example
int main()
{
int x = 6;
repchar(x, 'r');
printf("%d\n", x);
return 0;
}
void repchar(int n, char c)
{
while (--n >= 0)
putchar(c);
}
This program prints the letter r six times, and then at the last printf, prints out 6, not -1. The reason is that when repchar was called, x was copied. That way, when repchar decrements n, the caller's copy is not changed.
If we passed a pointer, however, n would be modified.
int main()
{
int x = 6;
repchar(&x, 'r');
printf("%d\n", x);
return 0;
}
void repchar(int *n, char c)
{
while (--(*n) >= 0)
putchar(c);
}
Instead of the variable being copied, now the address of the variable is being copied. Inside of repchar, *n is being counted down. This accesses the value that is being referenced by n, which is the same address as x and decrements it. As a result, the last printf will give -1.

Why can't I swap memory address of two variables using a function? C

static void swapAddr(int *numOne, int *numTwo)
{
int *tmp;
tmp = numOne;
numOne = numTwo;
numTwo = tmp;
}
int main(void)
{
int a = 15;
int b = 10;
printf("a is: %d\n", a);
printf("Address of a: %p\n", &a);
printf("b is: %d\n", b);
printf("Address of b: %p\n", &b);
swapAddr(&a, &b);
printf("\n");
printf("a is: %d\n", a);
printf("Address of a: %p\n", &a);
printf("b is: %d\n", b);
printf("Address of b: %p\n", &b);
return 0;
}
When I compile and run this piece of code, the output is
a is: 15
Address of a: 0x7fff57f39b98
b is: 10
Address of b: 0x7fff57f39b94
a is: 15
Address of a: 0x7fff57f39b98
b is: 10
Address of b: 0x7fff57f39b94
Clearly the result is not what I intended, since the address does not seem to have been swapped at all.
You generally can't change the address of a variable.
Your 'swapAddr' function changes its parameter values, but these are local to the function - you're not changing anything outside the function. Perhaps the best way of understanding it is that a function parameter always receives a copy of the value that was passed to the function. In this case, you get a copy of the address of a and a copy of the address of b. You can and do change the values of the variables holding those copies (numOne and numTwo), and seeing as they are pointers you could (but don't) change the values that they point at (the values of variables a and b) - but you can't change the addresses of the original variables.
To break it down:
static void swapAddr(int *numOne, int *numTwo)
{
int *tmp;
tmp = numOne;
At this point, tmp and numOne both point to the value of the a variable...
numOne = numTwo;
Now, numOne points instead to the value of the b variable...
numTwo = tmp;
}
And finally, numTwo now points to the value of the a variable. The function returns and numOne and numTwo no longer exist after that point. The addresses of the variables a and b did not change at any stage.
You could however write a function which exchanges the addresses in two pointer variables:
static void swapAddr(int **ptrOne, int **ptrTwo)
{
int *tmp;
tmp = *ptrOne;
*ptrOne = *ptrTwo;
*ptrTwo = tmp;
}
This would allow you to pass the address of two pointer variables, and on return the pointers would be swapped - each one pointing at what the other did previously. But again, this would not change the address of any variable that those pointers happened to point to.
The pointers are passed to the function by value, so changing what they point to isn't going to change the value of the passed parameters in the calling function.
When the function is called, a copy of each pointer is made and saved to the stack. Then the function reads each pointer value off the stack and manipulates them. It never changes the value of the original pointer that was copied onto the stack.
Remember that in C values are passed by value to functions, meaning that the values are copied. When you modify an argument in a function you only modify the local copy inside the function, not the original value that was passed to the function. This goes for pointers as well.
To solve your problem you must pass the arguments by reference, but unfortunately C doesn't have that, it only have pass by value. However, pass by reference can be emulated by passing pointers to the data, just like you do in the function. You must however dereference the pointer to get the values from where the pointers point to, and use those values to do the actual swapping:
int temp = *numOne; // Note: temp is a value not a pointer
*numOne = *numTwo;
*numTwo = temp;
static void swapAddr(int *numOne, int *numTwo)
In this function you are passing 2 pointers by value. This allows you to modify the int pointed to by the pointers but not the pointers themselves.
Use this function definition instead that passes pointers to pointers and allows modifying the pointers themselves
static void swapAddr(int **numOne, int **numTwo) {
int *tmp = *numOne;
numOne = *numTwo;
numTwo = tmp;
}
You could use it like this for example:
int *a = malloc(sizeof(int));
int *b = malloc(sizeof(int));
*a = 15;
*b = 10;
swapAddr(&a, &b);
You canlt change the addresses. The adderss of a is the address of a and that will remain the same until the end of days.
You can do:
static void swapAddr(int **numOne, int **numTwo)
{
int *tmp;
tmp = *numOne;
*numOne = *numTwo;
*numTwo = tmp;
}
int main(void)
{
int a = 15;
int b = 10;
int *pa= &a;
int *pb= &b;
swapAddr(&pa, &pb);
}
What you want to achieve is something like
int *c = &a;
&a = &b;
&b = &a;
This is not possible (you can check: it will not compile). A variable that is created is placed at one place in memory and stays there. So when you create a variable a it will stay variable a and it will not be able to change its identity to that of another variable b.
What you can do is use two pointers int *p1, *p2 to int. These pointers can change their value and point to other objects during lifetime:
p1 = a;
p2 = b;
p1 = b;
p2 = a;
a and b will stay the same, but p1 and p2 can point to different objects over time.
So a thing that would be possible:
static void swapaddr(int **pp1, int **pp2)
{
int *pp;
pp = *pp1;
*pp1 = *pp2;
*pp2 = pp;
}
int main(void)
{
int a = 15, b = 10;
int *pA = &a, *pB = &b;
swapAddr(&pA, &pB);
}
In this example a and b would keep their identity and address, but pA and pB would change their value and pA would point to b and pB would point to pA.
You cannot change the addresses of the variables.however you can change values of pointers,which store addresses as their value,here is an example :
#include <stdio.h>
void swapAddr(int **numOne, int **numTwo)
{
int *tmp;
tmp = *numOne;
*numOne = *numTwo;
*numTwo = tmp;
}
int main(void)
{
int a = 15;
int b = 10;
int *p_a = &a;
int *p_b = &b;
printf("Address of a: %p\n", p_a);
printf("Address of b: %p\n", p_b);
swapAddr(&p_a,&p_b);
printf("\n");
printf("p_a : %p\n",p_a);
printf("p_b : %p\n",p_b);
return 0;
}

Resources