pointer displaying different value than expected - c

I expected the output of the following program to to be 5, but the compiler is displaying 20. Can someone please explain why?
#include <stdio.h>
int a=5;
change1(int *p);
int main(void)
{
int x=20,*ptr=&x;
change1(ptr);
printf("%d ",*ptr);
return 0;
}
change1(int *p)
{
p=&a;
}

You're passing a pointer, which causes the function to make a copy. In order to change it you have to pass a pointer to a pointer.

If you want to modify a pointer, you need to pass a pointer to pointer:
change1(&ptr);
and then:
void change1(int **p)
{
*p = &a;
}

Related

How do you update a pointer that has to go through 3 functions?

Let's say I have a pointer inside function1, I need to pass it to function2, and function2 needs to pass it to function3, function3 needs to update that pointer. How do you correctly do this?
Do you use triple stars? If so, what's the correct way to pass the pointer inside function2?
function1(void){
char *pointer;
function2(&pointer)
}
function2(char **pointer){
function3(&(*pointer));
}
function3(char ***pointer)}
/*update pointer*/
}
My question is specifically about this situation only (3 functions), I don't want to remove function2, even if it does nothing.
Just keep two levels of pointer.
#include <stdio.h>
static int b = 42;
void foo(int **a) {
*a = &b;
}
void bar(int **a) {
foo(a);
printf("%d\n", **a);
**a = 21;
}
int main()
{
int *a = NULL;
bar(&a);
printf("%d\n", *a);
}
Output:
42
21

is it possible? passing address of local static value to main pointer?

#include<stdio.h>
void f(int *p) {
static int data = 5;
p=&data;
}
int main(void) {
int *ip=NULL;
f(ip);
printf("%d\n", *ip);
return 0;
}
if it is possible.
what is cause error?
how can I fix the code?
In this way you end up changing the value of a local pointer, you need to pass a pointer to pointer (&) from main and use the dereference operator (*) in the function:
#include <stdio.h>
void f(int **p) {
static int data = 5;
*p = &data;
}
int main(void) {
int *ip = NULL;
f(&ip);
printf("%d\n", *ip);
return 0;
}
But usually we prefer to work with the same level of indirection returning the address from the function, this is easier to read (at least for me):
#include <stdio.h>
int *f(void) {
static int data = 5;
return &data;
}
int main(void) {
int *ip = f();
printf("%d\n", *ip);
return 0;
}
You have to pass a pointer to the pointer to change the value of the actual pointer:
void some_fun(int **p)
{
static int i = 10;
*p = &i;
}
That being said, it is not necessarily advisable to do that. The only direct use I could think of is to delay the execution of the initialization of a global until its first use.

Value of variable changing when passing it to another function by reference

I have a project in which I have to make a basic database in C. My problem lies that keep passing a variable n from function to function and at one point the value changes.
I have made a test program which illustrates the same issue I'm having:
#include <stdio.h>
#include <stdlib.h>
void functionTWO(int *n)
{
printf("\n%d",*n);
}
functionONE(int *n)
{
int i=0;
i++;
i++;
*n = i;
printf("\n%d", *n);
functionTWO(&n);
}
int main()
{
int n=0;
printf("%d",n);
functionONE(&n);
return 0;
}
The n in the second function is displayed as a very very high number, e.g.:
0
2
2752268
Now, I know this probably is intended, but could you guys kindly explain why this happens this way?
In your functionONE(), n is already a pointer. You don't need to pass the address of the pointer if you intend to change that value-at-the-address.
As an advice, always check the data types and enable the compiler warnings to help you.
functionONE()
your are passing a address of address. So it's printing address of n.
Now you want a correct output then n as double pointer **n.
void functionTWO(int **n)
{
printf("\n%d",**n);
}
First of all, in C int & n is not the reference of n, it is it's address.Now, what you've done is created an int - sent it's address to functionONE - did something to the value in that address and then sent the the argument's address to functionTWO, instead of sending the argument itself, becuase it's already a pointer. Do this instead:
#include <stdio.h>
#include <stdlib.h>
void functionTWO(int *n)
{
printf("\n%d",*n);
}
functionONE(int *n)
{
int i=0;
i++;
i++;
*n = i;
printf("\n%d", *n);
functionTWO(n); // <-- send the pointer, not it's address
}
int main()
{
int n=0;
printf("%d",n);
functionONE(&n);
return 0;
}
In functionONE, n is int *, just a pointer, when you do functionTWO(&n);, you get the address of n, so it output a big value.
The value being printed in functionTWO function is the address of the pointer variable n declared in functionONE.In that case you can either modify functionTWO like this
void functionTWO(int **n)
{
printf("\n%d",**n);
}
or
Just pass the value of the pointer variable like this
functionTWO(n);

Why am I not able to access values that were stored in another function?

Basically, why does it not just print the integers that are entered. Right now it just prints garbage value, but I do not know why it cannot access the values stored after it leaves the function. It only seems to get messed up after leaving the getIntegersFromUser function. If I run the for loop in the getIntegers function it does it properly, but why not in the main function?
Thanks in advance for your help.
#include <stdio.h>
#include <stdlib.h>
void getIntegersFromUser(int N, int *userAnswers)
{
int i;
userAnswers =(int *)malloc(N*sizeof(int));
if (userAnswers)
{ printf("Please enter %d integers\n", N);
for (i=0;i<N; i++)
scanf("%d", (userAnswers+i));
}
}
int main()
{
int i, M=5;
int *p;
getIntegersFromUser(M, p);
for (i=0;i<5;i++)
printf ("%d\n", p[i]);
return 0;
}
Also, this is a homework question, but it's a "Bonus Question", so I'm not trying to "cheat" I just want to make sure I understand all the course material, but if you could still try to give a fairly thorough explanation so that I can actually learn the stuff that would be awesome.
Pointers are passed by value. The function is using a copy of your pointer, which is discarded when the function ends. The caller never sees this copy.
To fix it, you could return the pointer.
int *getIntegersFromUser(int N)
{
int *userAnswers = malloc(...);
...
return userAnswers;
}
/* caller: */
int *p = getIntegersFromUser(M);
Or you could pass your pointer by reference so the function is acting on the same pointer, not a copy.
void getIntegersFromUser(int N, int **userAnswers)
{
*userAnswers = (int *) malloc(N*sizeof(int));
...
}
/* caller: */
int *p;
getIntegersFromUser(N, &p);

Passing address as function argument

Which of these codes is appropriate and why ?
code1:
void fun(int *p)
{
*p=200;
}
main(){
int *i_ptr;
fun(i_ptr);
printf("%d", *i_ptr);
}
Code2:
void fun(int *p)
{
*p=200;
}
main(){
int i;
fun(&i);
printf("%d", i);
}
is it okay to pass pointer's address ?
You asked:
Which of these codes is appropriate and why ?
The first one will lead to undefined behavior since i_ptr has not been initialized to point to a valid memory.
You can change it to:
int main(){
int i;
int *i_ptr = &i;
fun(i_ptr);
printf("%d", *i_ptr);
}
and it will be OK.
BTW, I also added int as the return type of main.
You also asked:
is it okay to pass pointer's address ?
It is clear to me how the question is related to the code you posted.

Resources