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.

Recommended Answers

All 2 Replies

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

What's wrong with the standard form:

int change (int k)
{
    if (k==1) k=0;
        else  k=1;
    return k;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.