Question on Pointer tutorial

Apr 1, 2014 at 8:56am
Reference http://www.cplusplus.com/doc/tutorial/pointers/

I can't seems to compile "foo = &myvar;" but I can compile "*foo = &myvar;"
Did I miss out any std files?

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include<stdio.h>
using namespace std;

int main()
{
int myvar = 25;
int foo = &myvar;
int bar = myvar;
int beth = *foo;
return(0);

}
Apr 1, 2014 at 9:02am
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int myvar = 25; // type of myvar is 'int'
    
    // int foo = &myvar;
    int* foo = &myvar; // type of foo is 'pointer to int'
    
    int bar = myvar; // type of bar is 'int'
    
    int beth = *foo; // type of beth is 'int'
}
Apr 1, 2014 at 11:10am
u cant assign address of a variable to a normal variable as suggested by @JLBorgers u need to use pointer of same type

int *foo = &myvar;
Apr 2, 2014 at 2:55am
i.e the tutorial is wrong??
Apr 2, 2014 at 3:03am
The tutorial simply omits the types. You inferred them all to be int, which was incorrect. The tutorial intended for you to assume the types were those given by JLBorges.
Apr 3, 2014 at 1:10am
ah...ok. got it. thanks!
Topic archived. No new replies allowed.