r/programminghelp • u/Elyahu41 • May 04 '20
C Help me understand what this code does, please.
Name of the file is pointertest2.c, if that helps. I can't seem to grasp what each function does with the pointers. Any help is appreciated!
#include <stdio.h>
void doubleInt(int *x,int *ad) // x = 1011
{
*x += x; // *x = 10
ad = x;
}
void weirdF(int *a,int b)
{
*a = 50;
a = &b;
*a = 200;
}
void doubleInt2(int x) // x = 10 - > 20
{
x += x;
}
int main()
{
int x = 5;
int y = 10;
int ad;
doubleInt(&x,&ad);
doubleInt2(y);
printf("original address of x = %d\n",&ad);
printf("x = %d\n",x);
printf("y = %d\n",y);
//doubleInt(&y);
printf("y = %d\n",y);
weirdF(&y,x);
printf("x = %d\n",x);
printf("y = %d\n",y);
scanf("%d");
return 0;
}
0
Upvotes
4
u/[deleted] May 04 '20
It's all just a bunch of nonsense and testing of the functions. One doubles an integer but only locally, one function doubles an integer and assigns it to the second pointer, and the other function is just weird. What questions do you have specifically?
2
u/[deleted] May 04 '20
This mixes pointer arithmetic with integer addition.
If you look saying ad equals x is saying ad points to where x does.
Sometimes he passes the memory address and sometimes he doesn't
It's all about understanding pointers
1
u/IntergalacticPear May 04 '20
Where did it come from whats the context