Variable passed as a parameter won't increment, why? [duplicate] - c

This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 11 months ago.
Here I wrote this dummy code to show what my problem (in a more complex code) is. I am using implicit functions with a parameter. The function should assign a new value to the variable passed as a parameter, but I noticed that while this works passing the address of the variable (i.e. the parameter is a pointer, variable i_1 in my code), it won't work passing the variable itself (i_2 in this code). Why does this happen?
#include <stdio.h>
#include <stdlib.h>
void plusfivepointer(int * pa)
{
*pa+=5;
}
void plusfive(int a)
{
a+=5;
}
int main(void)
{
int i_1, i_2;
i_1=i_2=0;
printf("i_1=%d i_2=%d\n", i_1, i_2);
plusfivepointer(&i_1);
plusfive(i_2);
printf("i_1=%d i_2=%d\n", i_1, i_2); //i_2 won't increment
return EXIT_SUCCESS;
}

Because when you don't pass a variable by reference the function creates a local variable with the same name and works the process defined in the function on that local variable and that variable vanishes when the function pop out of the stack.
You need to pass by reference to alter the variable you passed in the function.

Related

Can parameters have same name as a global variable and still called inside a function? [duplicate]

This question already has answers here:
How can I access a shadowed global variable in C?
(6 answers)
Closed 2 years ago.
#include<stdio.h>
int x = 12;
void foo(int x)
{
printf("%d\n",x);
}
int main()
{
foo(3);
printf("%d\n",x);
return 0;
}
This gives output
3
12
How can i access global int x inside the function foo()?
You can, but the parameter shadows it. Just use another name for parameter variable in foo function.
Yes it works like this as variables of same names are allowed provided that they are in different scopes. "x" is both defined in global scope (global variable) and a local scope (in foo() as a parameter). But they are located in different locations in memory.

main function in C with void and without void [duplicate]

This question already has answers here:
func() vs func(void) in C99
(4 answers)
Difference between int main() and int main(void)?
(10 answers)
Closed 4 years ago.
What is the difference between these two programs?
The 1st one i am getting 4,3,2,1 and 2nd one is compilation error.
#include <stdio.h>
int main()
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
and
#include <stdio.h>
int main(void)
{
static int i = 5;
if (--i){
printf("%d ", i);
main(10);
}
}
When you define a function like this:
int func() { ... }
It says that the function takes an indeterminate number of arguments and returns an int. So you can legally pass any number of arguments of any type (although you won't be able to access them).
When you define a function like this:
int func(void) { ... }
It says that the function takes no arguments. Attempting to pass any arguments to this function will result in a compile time error.
On a side note, recursively calling the main function is not a good idea. You're better off either calling another function which is recursive or just using a loop.
The appearance of a solitary void in a parameter list explicitly tells the compiler "this function takes no arguments".
In the first code example, calling main recursively is permitted since there is no argument list, which permits any number of arguments (this may have been changed in a more recent C standard than the one supported by your compiler; I forget the specifics).
Variables declared static are stored in the process' data section rather than in stack memory, so they persist beyond their scope and retain their value across function calls, so i decrements on each call until it reaches zero and your program hits the base case (don't enter the if statement), and terminates.

pass function as parameter with and withou ampersand c [duplicate]

This question already has answers here:
Function pointers and address of a function
(5 answers)
Closed 4 years ago.
I want to pass a function as parameter, but I am confused if I should pass it with ampersand or not. The following snippet works in both ways. Why?
#include <stdio.h>
int print(int pas){
return pas;
}
int func_print(int (*k)(int)){
(*k)(555);
}
int main(){
printf("%d",func_print(print));
printf("%d",func_print(&print));
return 0;
}
Function names are special.
When used without calling the function, it automatically is translated to a pointer to the function. So &print, print, *print, **print, ***print, etc. all evaluate to the same expression of type int (*)(int).

Is passing pointers to functions similar to calling a function by reference? [duplicate]

This question already has answers here:
Passing by reference in C
(19 answers)
Closed 4 years ago.
I don't really see any difference, with passing pointers to functions and calling a function by reference. Am I right
#include <stdio.h>
int multi;
int multiplication(int *a, int *b){
multi = (*a) * (*b);
return multi;
}
int main()
{
int X = 2, Y=3;
multiplication(&X, &Y);
printf("%d", multi);
return 0;
}
From the example code you show, you obviously mean pass by reference, not call by reference.
There is no pass by reference in C, it's always pass by value. Of course, you can get the effect of pass by reference by passing a pointer to something. The pointer is your reference. It's again passed by value, but you use that value to access some other object.
Languages that have pass by reference will typically use pointers to implement it.

Pass by reference and pointers [duplicate]

This question already has answers here:
What's the difference between passing by reference vs. passing by value?
(18 answers)
Closed 8 years ago.
What is the difference between passing by reference the parameters in a function and passing pointer variables as a parameter in a function ?
There is no pass by reference in C, it's always pass by value.
C developers can emulate pass by reference, by passing the pointers to a variable and the accessing it using dereferencing within the function. Something like the following, which sets a variable to 42:
static void changeTo42 (int *pXyzzy) {
*pXyzzy = 42;
}
:
int x = 0;
changeTo42 (&x);
Contrast that with the C++ true pass by reference, where you don't have to muck about with pointers (and especially pointers to pointers, where even seasoned coders may still occasionally curse and gnash their teeth):
static void changeTo42 (int &xyzzy) {
xyzzy = 42;
}
:
int x = 0;
changeTo42 (x);
I would implore ISO to consider adding true references to the next C standard. Not necessarily the full capability found in C++, just something that would fix all the problems people have when calling functions.
You might be thinking of C++. I'll cover that below.
In C there is no passing by reference. To accomplish the same feat, you can send a pointer to a variable as an argument and dereference the pointer in the method, as shown in paxdiablo's comment.
In C++, you could accomplish the same thing if you tried to pass by reference C-style (as explained previously) or if you tried passing the arguments as such:
static void multiply(int& x){
x * 7;
}
void main(){
int x = 4;
multiply(x);
}
The variable x at the end of this program would equal 28.

Resources