Passing a pointer from a function to another function - c

I am looking for a way to pass a pointer from not main function to another function. A variable x with value 5 sends as pointer to func1, func1 changes variable value to 10, then func1 sends same variable to func2.
I want it to look something like that:
#include <stdio.h>
void func1(int *px);
void func2();
int main(void)
{
int x=5;
printf("x in main %d\n", x);
func1(&x);
return 0;
}
void func1(int *px)
{
*px = 10;
printf("x i funk 1 %d\n", *px);
funk2(); // send to this function a pointer to x, which in this function(func1) is pointed by *px
funk3();
}
void func2()
{
//*px=15 // here I want to change value of x
}

I am completely lost in how to do that.
Using the exact same logic you used for passing the pointer to x from main to func1.
In this case func2 should also accept a pointer to intand func1 should pass the pointer to func2 that it got from main:
#include <stdio.h>
void func1(int *px);
void func2(int *px);
int main(void)
{
int x=5;
printf("x in main %d\n", x);
func1(&x);
// x will be 15 here
return 0;
}
void func1(int *px)
{
*px = 10;
printf("x i funk 1 %d\n", *px);
funk2(px); // send to this function a pointer to x, which in this function(func1) is pointed by *px
//funk3(); // this function was not defined anywhere
}
void func2(int *px)
{
*px=15;
}

Simply pass the argument in func1() to func2(), as a pointer, px in func1() and py in func2() will point to the same memory location, the address of x.
int main(void)
{
int x = 5;
printf("x in main %d\n", x);
func1(&x);
printf("x now %d\n", x);
return 0;
}
void func2(int *py)
{
*py = 15; // here I want to change value of x
}
void func1(int *px)
{
*px = 10;
printf("x in funk 1 %d\n", *px);
func2(px); // send to this function a pointer to x, which in this function(func1) is pointed by *px
}
output:
x in main 5
x in funk 1 10
x now 15

Related

f(&a) is possible to run in C?

The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0
An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.

Pointer to pointer as a function parameter

I have the following code:
#include <stdio.h>
#include <stdlib.h>
int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
int main(void) {
int c = 4;
printf("f(): %d\n", f(c, &c, &&c));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
How can I access **ppz correctly, because so I get an error message: "label 'c' used, but not defined".
An int** is a pointer to an int*. You need to create a variable of type int* to be able to pass a pointer to it somewhere. Here is what you should do:
int main(void) {
int c = 4;
int* pc = &c;
printf("f(): %d\n", f(c, pc, &pc));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
Refer #ikegami's answer for an explanation of the proper use of a pointer to a pointer.
You want to modify a variable of type int, so the parameter should be int *, not int **.
You'd use int ** if you wanted to modify a variable of type int * variable. That's not the case here.
For example,
void f(int **pp) {
*pp = malloc(10);
}
int main(void) {
int *p;
f(&p);
// ...
free(p);
}

Why my pointer code is giving me wrong output in C?

Why my pointer code is giving me wrong output ?.
Where, my actual code is :
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", x, y);
}
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}
My expected output is like this : 6 7 6 7
It's giving me output like this : 6 7 50 100
Thanks.
In fun1 the expression &x is a pointer to the pointer. It's of type int **.
You should not use the address-of operator there, since x and y already are pointers.
An easy way to get the compiler to warn you about this is to declare thje function fun2 before you use it:
// Declare fun2 prototype
void fun2(int *x, int *y);
void fun1(int *x, int *y)
{
...
}
// Define fun2 implementation
void fun2(int *x, int *y)
{
...
}
Before use void fun2(int*, int*), you must declare or define it
In funxtion fun1, the line printf("%d %d ", x, y); should be printf("%d %d ", *x, *y);, for x and y are int*
In function fun1, the line fun2(&x, &y); should be fun2(x, y); for x and y are int*
The following code could work:
#include <stdio.h>
void fun2(int *x, int *y){
*x = 6;
*y = 7;
}
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(x, y);
printf("%d %d\n", *x, *y);
}
int main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d\n",x,y);
return 0;
}
Method 1. Point To Point To Int
void fun1(int *x, int *y){
*x = 50;
*y = 100;
fun2(&x, &y);
printf("%d %d ", *x, *y);
}
void fun2(int **x, int **y){
**x = 6;
**y = 7;
}
void main()
{
int x = 5;
int y = 10;
fun1(&x, &y);
printf("%d %d",x,y);
}

C pointer---function relation [duplicate]

This question already has answers here:
Call by reference, value, and name
(6 answers)
Closed 5 years ago.
We called the function foo(&x, &y) in the main function but then why did we call swap (x, y) inside void foo function as swap (&x, &y)?
#include <stdio.h>
#include <stdlib.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void foo(int*p1, int*p2) {
*p1 += *p2;
swap(p1, p2);
*p2 *= *p1;
}
int main(){
int x = 5, y = 9;
foo(&x, &y);
printf("x=%d\n", x);
printf("y=%d\n", y);
system("PAUSE");
return 0;
}
The difference is the type of the parameters you call the function with.
First case:
The type of x is int and the foo function is expecting int *.
So you have foo(&x, &y);
Second case:
The type of p1 is int * and the swap function is also expecting int *.
So you have swap(p1, p2);

Function to increment the value of a variable in C

I wrote the same code in PHP, the value of 5 got incremented, but
why doesn't the value get incremented in C?
int foo(int x){
x++;
}
int main( ){
int y = 5;
foo(y);
printf("value of y = %d\n", y);
}
In C passing arguments is done by value rather than by reference. That is, the number that the function is working with is unique to the one passed to and has its own space in memory. To get around this do
int foo(int* x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
You should pass the parameter by reference to function foo.
int foo(int *x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
Because x, y are local variables to their functions. The scope of the variable lies within the block of the function. So you can't change the value of those variables from outside the block.
Solution:
Either you declare those variables as global variables
int y;
int foo(){
y++;
}
int main( ){
y = 5;
foo();
printf("value of y = %d\n", y);
}
Or using reference you can do that
int foo(int *x){
(*x)++;
}
int main( ){
int y = 5;
foo(&y);
printf("value of y = %d\n", y);
}
Might be in PHP it is directly pass-by-reference. But C has different function calls.
If you want to increment the value in the function and it should reflect in the main function. There are two possible ways :
1.
int foo(int x)
{
return ++x; //returning the incremented value
}
2.
void foo(int *x)
{
++(*x);//this function is pass-by-reference.
}
Please let me know if there are any issue.
What you could also be doing if you want to avoid using pointers is as below
int addone(int n);
int main(void) {
int n=0;
printf("Before: %d\n", n);
n=addone(n);//assign the returned value from function to the variable 'n'
printf("After: %d\n", n);
return 0;
}
int addone(int n) {
n++;
return n;
}

Resources