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;
}
Related
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.
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 ?.
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);
}
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
I am trying to a simple addition function that will take a number x from an array an and add a variable i to it, which is the iteration variable inside the for loop. Instead of adding each variable individually and producing the output of :
3, 2, 7
it produces values of
3, 5, 13.
#include <stdio.h>
int add(int x[], int y);
int main(void) {
int i;
int a[3] = {3, 1, 5};
for(i=0; i<3; i++)
{
printf("%d \t", i);
printf("%d, %d, equals %d \n", a[i], i, add(a[i], i));
}
return 0;
}
int add(int x[], int y){
return x+y;
}
Try
int add(int x, int y){
return x+y;
}
In your function definition,
int add(int x[], int y){
int x[] expects an array to be passed as the argument. By calling that with
add(a[i], i)
you're making multiple mistakes, like
passing an int value where int * is expected, so a wrong and implicit conversion from int to int * is talking place.
Inside add(), x is of type int *. So, by saying return x+y;, you're essentially again (wrongly) converting a pointer to int type.
Here, it seems, you need only one int variable. not an array. Change it (both declaration and definition) to
int add(int x, int y){
Suggestion: Turn up the compiler warnings and pay heed to them. For example, with the -Wall option enabled, you would have received a warning saying
warning: passing argument 1 of ‘add’ makes pointer from integer without a cast
printf("%d, %d, equals %d \n", a[i], i, add(a[i], i));
^
and
expected int * but argument is of type int
int add(int x[], int y);
and,
warning: return makes integer from pointer without a cast
return x+y;
^
You are using array as an argument. Please try below code:
#include <stdio.h>
int add(int x, int y);
int main(void) {
int i;
int a[3] = {3, 1, 5};
for(i=0; i<3; i++)
{
printf("%d \t", i);
printf("%d, %d, equals %d \n", a[i], i, add(a[i], i));
}
return 0;
}
int add(int x, int y){
return x+y;
}