Function Pointer

I'm trying to a change the value of a variable inside the function via a pointer. But nothing i try seem's to work.
The error i'm getting is:

G:\DCSTemp\new\main.cpp||In function 'int main()':|
G:\DCSTemp\new\main.cpp|11|warning: unused variable 'reset' [-Wunused-variable]|

The code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

void reset(int *ip)
{
    *ip = 0;
};

int main()
{
    int i = 25;
    int reset(i);
    std::cout << "i = " << i;
}
This

int reset(i);

is a declaration of a variable with name reset that is initialized by the value of variable i.

I think you wanted to call the function.

reset( &i );
Last edited on
Changing i to &i only gave me an error:

G:\DCSTemp\new\main.cpp|12|error: invalid conversion from 'int*' to 'int' [-fpermissive]|

I can change a value using a reference in the function parameter:

i.e. void reset(int &Var)

But i'm hitting problem's using a pointer.

As the next section in the book i'm learning from expand's on pointer usage,
Using const, Passing multidimensional array's etc... I need to have learnt the basic's before i go any further.
Where did you see a reference? Are you capable simply copy and paste the code I showed?!
Change

int reset(i);

to

reset( &i );

Last edited on
Thank's vlad.
Topic archived. No new replies allowed.