Does anyone know of a way I can change the values of variables that are defined locally?
#include <stdio.h>
int change(int x, int y);
int main()
{
int x = 10;
int y = 20;
change(x,y);
printf("x:%d y:%d\n", x, y);
}
int change(int x, int y)
{
x = 20;
y = 30;
return(x);
return(y);
}
I want x and y to print 20 and 30 in main().
I tried returning the values, but that didn't work. Is there another method I might be able to use? I was thinking pointers, but I don't know where to begin.
Use pointers:
void change(int *x, int *y)
{
*x = 20;
*y = 30;
}
and call the function like: change(&x, &y); For readability you may want to use different names than x and y for the change parameters as they are not of the same type as the x and y variables declared in main.
Some options:
1) Pass by refernce: int change(int &x, int &y);
2) Return an array and change current values:
int* temp = new int[2];
temp[0]=x+10;
temp[1]=y+10;
return temp;
3) Return to printf directly:
printf("x:%d y:%d\n", change(x,y)[0], change(x,y)[1]);
yes it's very easy
just pass the parameters by reference to the change function
so instead of
int change(int x, int y) >
int change(int& x, int& y)
and it will works fine
Related
A few lessons ago I learned about variables, and got a question in my homework about swapping two numbers - I used a third variable to solve this question.
The solution looked somewhat like this:
#include <stdio.h>
int main(void) {
int x, y;
scanf("%d %d", &x, &y);
// swappring the values
int temp = x;
x = y;
y = temp;
printf("X is now %d and Y is now %d", x, y);
}
Now I'm learning about functions, and I wanted to try and solve the previous question with a helper swap function.
This is the code I've written:
#include <stdio.h>
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main(void) {
int a = 3, b = 4;
swap(a, b);
printf("%d %d\n", a, b);
}
I don't know why, but the output is still 3 4 even though I changed the value inside the swap() function.
Why is this happening?
Pass address of x and y as arguments to function. Right now they are local variables, changes are not made to original variables .
Do as follows-
void swap(int *x,int *y){
/* dereference pointers and swap */
int temp = *x;
*x = *y;
*y = temp;
}
And call in main like this -
swap(&x,&y);
What you are doing is passing parameter by value. It means that during the function call, copies of parameters are created. So inside the function you are working on copies of actual variables.
Instead you need to pass it as a reference. Please read more about pass-by-value vs pass-by-reference.
#include <stdio.h>
void swap(int& x,int& y) //Instead of passing by value just pass by reference
{
int temp=x;
x=y;
t=yemp;
}
int main() {
int a=3,b=4;
swap(a,b);
printf("%d %d\n",a,b);
return 0;
}
EDIT:
C does not have references. Above code will work in c++ instead. To make in work in C, just use pointers and de-reference it inside the function.
So I have to create two functions. In the int main, the program asks to input an x and y value. On the first function, it adds 1 to the x and y value. And on the next function, it prints out the result from that function. And now my problem is that the 2nd function doesn't print out the output from the first function, I do not know what I'm doing wrong, here's my code so far:
#include <stdio.h>
void plusone(int *x,int *y);
void printresult(int *x, int *y);
int main()
{
int x,y;
printf("enter x variable: ");
scanf("%d",&x);
printf("enter y variable: ");
scanf("%d",&y);
plusone(&x,&y);
printf ("%d and %d\n\n",x,y);
printresult(&x,&y);
}
void plusone(int *x,int *y)
{
return *x+1;
}
void printresult(int *x,int *y)
{
printf("the result is: x:%d y:%d",*x,*y);
}
so when I run that program, enter 1 for x and 2 for y, the output is still 1 and 2, but it's supposed to be 2 for x and 2 for y after the plusone function, can I ask what's wrong with my code? Sorry for my english but I hope I explained it well, I am new to C programming
A better answer is that the plusone function is not applying any side-effect on the *x parameter. Here is a plusone function that has a side-effect (actually two--one on each variable passed in):
void plusone(int *x,int *y)
{
*x = *x+1;
*y = *y+1;
}
You are passing a pointer to plusone - that's a good start. However, you are not using that pointer to update the value passed in:
void plusone(int *x,int *y)
{
*x = *x+1;
}
or better
void plusone(int *x,int *y)
{
(*x)++;
}
Now you assign a new value to whatever x is pointing, that is, to the variable x of main.
Note: Since you were returning a value from a function declared void, the compiler should have given you a warning.
Note: although passing by pointer to plusone is necessary, you can pass by value to printresult, because the function does not need to change values of the local variables in main that you pass to it.
void plusone(int *x,int *y)
{
return *x+1;
}
should be
void plusone(int *x,int *y)
{
(*x)++; //or *x+=1; which is the short for *x=*x+1;
}
You cannot return an int for a function that does not return anything (void).But here,I don't think you were attempting to return a variable.You wanted the variable x to be incremented once.So just do that in your program just like I've done above.
You should not return from plusone, this is the right solution
#include <stdio.h>
void plusone(int *x, int *y);
void printresult(int x, int y);
int /* main() returns int */ main()
{
int x, y;
printf("enter x variable: ");
scanf("%d",&x);
printf("enter y variable: ");
scanf("%d",&y);
plusone(&x, &y);
printresult(x, y);
return 0; // main sholud return `int' as you see from it's definition.
}
void
plusone(int *x, int *y)
{
*x += 1;
*y += 1;
}
void printresult(int x, int y)
{
printf("the result is:\n\tx: %d\n\ty: %d\n", x, y);
}
You said,
it adds 1 to the x and y value_
in your plusone function you was passing int *y but never doing anything with it, and int *x but not updating it's value.
plusone is trying to return an int what it's return type is void. And it isn't even trying to change anything (no assignment statement).
If you're trying to manipulate the x and y variables directly, like I think you are, you need to do something like
*x += 1;
*y += 1;
in that function
How about this solution.
void plusone(int *x,int *y)
{
*x += 1;
}
I think your original source will make compile error due to return int in void function
Can someone please explain me the purpose of func(&_) and void? I am not sure how the whole program works.
void func(int *xp);
int
main(void)
{
int x, y;
x = 5;
func(&x);
y = x-3;
func(&y);
printf("%4d%4d\n", x, y);
return(0);
}
void
func(int *xp)
{
int y;
y = *xp * 2;
*xp = y - 3;
}
Your function demands a pointer argument. If you want to pass a non-pointer variable to that function call, you must provide it's address, using the & notation.
how to pass array as arguments in C?
int a,b,c[10];
void Name1(int x, int y, int *z)
{
a = x;
b = y;
c = z;
}
I try to pass it as argument, but it does not build, how to fix it?
And is the declaration of void Name1(int x, int y, int *z); is the same as void Name1(int x, int y, int z[])? does the void Name1(int x, int y, int z[]) will be treated as void Name1(int x, int y, int *z); by compiler?
When you pass array as an argument to function it decays as an pointer to its first element.
So,
void Name1(int x, int y, int *z)
will work.
But arrays are not assignable so:
c = z;
does not work, you will need to explicitly copy each array element from source to destination.
Doing c = z;, you're assigning to a non l-value, which is not allowed. It's like doing &c[0] = &z[0];.
There are a couple of answers here, but I don't think any of them are completely sufficient, so...
basically you can either copy the array or use the pointer, but either way you will need to keep track of the length.
int a,b, *c, len;
void Name1(int x, int y, int *z, int z_len)
{
a = x;
b = y;
c = z;
len = z_len;
}
//usage:
int arr[5];
Name1(1,2,arr /* or &arr[0] ,*/, sizeof(arr )/ sizeof (int));
if you never need to add items this will be sufficient... If you do it gets more complicated...
it is important to keep the length around so that you know how many elements you have, even if you are going to copy them.
c=z; is invalid
c pointer doesnt have memory to save the z address.
you can say:
int a,b,*c;
c=(int *)calloc(10,sizeof(int) );
void Name1(int x, int y, int *z)
{
a = x;
b = y;
c = z;
}
Hey all. I'm working on a project for school where I need to pass a few parameters by reference through multiple functions. I understand how I can pass by reference from where the variables are declared to another function, like this:
main() {
int x = 0;
int y = 0;
int z = 0;
foo_function(&x, &y, &z);
}
int foo_function(int* x, int* y, int* z) {
*x = *y * *z;
return 0;
}
However, how would I pass x, y, and z from foo function to another function? Something like this gives me all kinds of compiler warnings.
int foo_function(int* x, int* y, int* z) {
*x = *y * *z;
bar(&x, &y, &z);
return 0;
}
int bar(int* x, int* y, int* z) {
//some stuff
}
Just use:
bar(x, y, z);
X, Y, and Z are already pointers - just pass them directly.
Remember - a pointer is a location in memory. The location doesn't change. When you dereference the pointer (using *x = ...), you are setting the value at that location. But when you pass it into a function, you are just passing the location in memory. You can pass that same location into another function, and it works fine.
You don't need to do anything, they're already references.
int foo_function(int* x, int* y, int* z) {
bar(x, y, z);
return 0;
}
int bar(int* x, int* y, int* z) {
//some stuff
}
In foo_function x y and z are already pointers (int*), so you can do bar(x, y, z).
int foo_function(int* x, int* y, int* z) {
*x = *y * *z;
/* x, y and z are pointers to int
&x, &y and &z are pointers to pointer to int
bar expects pointers to int, so call bar as:
*/
bar(x, y, z);
return 0;
}
C has no notion of passing by reference. Parameters are always passed by value. However, when using pointers, this value is actually a pointer to your actual value.
But what you are doing with
foo_function(&x, &y, &z);
is actually trying to get an address of the pointer, which is essentially meaningless (you would pass an int** instead of an int*).
So,
foo_function(x, y, z);
would be the correct call, as x, y and z are already pointers and you don't need to make the pointing chain any longer :)