0

Aim: I am learning pointer of C,now I want to write a function to change the value of a variable .

#include<iostream>
using namespace std;
void change (int * k)
{
	if (*k==1) *k==0;
	else *k==0;

}
void main()
{
	int a=1;
	cout<<a<<endl;
	change(&a);
	cout<<a;

}

I thought that n passing the address of a variable to the function can changes the value of k,but failed . I am eager to figure out the details of the arguement passing.
So, my dear friends, I really need yours help.

3
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by WaltP
0

The code you posted is in c++ format and not in standard c format

now for assigning...

if (*k==1) *k==0;
else *k==0;

== is used to check if the values are equal
= is used to assign a value to a variable

0

What's wrong with the standard form:

int change (int k)
{
    if (k==1) k=0;
        else  k=1;
    return k;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.