I was trying to change the adresses of two pointers (one by another) instead of changing their values.
The goal was to do it with arrays, but here's the thing:
I made it work with pointers, but it kinda didn't work with arrays.
(Plus: Some values of the arrays are changed, but not their adresses.)
Could you help me understand why?
Here are the codes:
Pointers:
Code:
#include<stdio.h>
#include<stdlib.h>
void invert_ad (int** a, int** b)
{
int* temp;
temp = *a;
*a = *b;
*b = temp;
}
main()
{
int a = 10;
int* pointer_a = &a;
int** ppointer_a = &pointer_a;
int b = 20;
int* pointer_b = &b;
int** ppointer_b = &pointer_b;
printf("pointer_a Adress: %p\n", pointer_a);
printf("pointer_b adress: %p\n", pointer_b);
invert_ad (ppointer_a, ppointer_b);
printf("pointer_a adress: %p\n", pointer_a);
printf("pointer_b adress: %p\n", pointer_b);
}
Arrays:
Code:
#include<stdio.h>
#include<stdlib.h>
void invert_ad (int** a, int** b)
{
int* temp;
temp = *a;
*a = *b;
*b = temp;
}
main()
{
int array_a[5] = {0, 1, 2, 3, 4};
int array_b[5] = {5, 6, 7, 8, 9};
int** parray_a = &array_a;
int** parray_b = &array_b;
printf("array_a adress %p\n", array_a);
printf("array_b adress %p\n", array_b);
invert_ad (parray_a, parray_b);
printf("array_a adress %p\n", array_a);
printf("array_b adress %p\n", array_b);
}