Question about delcaring pointers

So I have two questions about pointers. Why can't I output *p off the bat without equaling it to something first e.g.:*p=&x......and I was under the impression that *p was related to p so why can't p be output to the screen if it has been declared on line 1?
1
2
3
4
5
6
7
8
        int *p;
        int x;

        p=&x;
        x=100;

        cout<<*p<<endl;//can't output p for some reason...look up, study
        cout<<x<<endl;///*p and x are the same 


If I try to just output p, the compiler log tells me invalid conversion from int to int

EDIT: I just played around with the code and found that variable p is equal to the memory address. So....is it true that p can't be changed because if it was, p would no longer exist being that i changed the memory address?
Last edited on
Your edited question is ambiguous.

I've also noticed that you're not using the scope operator :: prefix to cout and endl . Assuming you're using the using keyword, I discourage you from doing this as it pollutes the standard namespace.

I ran the code and everything seemed to be running fine.
p points to an integer, and you assigned the address of x to p.
Then p was dereferenced and the integer it was pointing to, x, streamed into the console.
I don't know if you played with the code back on your computer or the code here? It would have been better if you kept the original here as for future reference.

Anyway, as the code is now, both *p and x output 100.


So....is it true that p can't be changed because if it was, p would no longer exist being that i changed the memory address?


I didn't understand this. Anyway, p is a pointer, it is located at &p, and its value is &x in this scenario. Now, when you create a pointer to an int for example, 4 bytes of space is allocated on a x86 machine (32-bit). But, without having it point somewhere, the bits in these 4 bytes make no sense and are more likely to point to somewhere restricted (called illegal location).
Last edited on
mmm, you're snippet looks right, but here's a whole program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main(void)
{
	int *p;
	int x;
	
	p = &x;
	x = 100;
	
	std::cout << *p << std::endl;
	std::cout << x << std::endl;
	
	return 0;
}


It's tough to explain without getting very basic. You declare a variable, and that variable has a type, a location in memory and a size. The type defines the way the value in that memory is interpreted.

In this way, pointers and "regular" variables are exactly the same. The confusing part is that the value in a pointer's memory is the address of a variable of the same type. The pointer has it's own place in memory:
1
2
3
4
5
6
7
8
9
10
int *p
int x;

p = the value of p
*p = the dereference
&p = the address of p

x = the value of x
// can not be dereferenced
&x = the address of x

Last edited on
Thanks and yes the code works fine. I have the code I tinkered with below. My major concern was how come I can't declare p to a number. e.g.:

If I try to declare p to say...50 p = 50, the compiler gives me an error saying invalid conversion from int to int. It's also on line 12

@Nexius..how does the using keyword pollute the namespace and what would be an alternative?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>/*Working with pointers*/ ///Chapter 14
#include <string>
using namespace std;

int main()
{
        int *p;
        int x;

        p=&x;
        x=100;
        p=50;//the compiler won't let me declare p at all. Everything else is fine.

        cout<<*p<<endl;//p is the memory address so I understand why It cant equal another number because p would probably no longer exist
        cout<<x<<endl;

        x=1500;

        cout<<"\nThese are the 3 p's.....memory address, pointer address, and variable p respectively"<<endl<<endl;
        cout<<"&p= "<<&p<<endl;
        cout<<"*p= "<< *p<<endl;
        cout<<"p= "<<p<<endl<<endl;

        cout<<"x has been equaled to 1500"<<endl<<endl;

        cout<<"x= "<< x<<endl;
        cout<<"*p= "<<*p<<endl;

        return 0;
}
Last edited on
The reason why the compiler is complaining at p = 50 is because you're trying to assign an integer to a pointer to an integer. You're assigning a different type and so the compiler rightfully knows they should be incompatible.

you can fix this problem by casting 50 to a pointer via C-style cast like so: int* pint= (int*)50;

However, doing this is dangerous as you could be fiddling with memory you don't know. A static_cast is also a good alternative, you should look into it.

using namespace std; Is bad practice because you are practically importing the whole standard namespace, thus opening up a lot of possibilities for name clashes. It is better to import only the stuff you are actually using in your code, like the cout and cin .

Use the prefix std:: for every function you may need to use.
std::cout << "This is considered better practice than \"using\"!" << std::endl;

If this sounds too tedious and you absolutely know you don't have any functions named cout, cin, or endl, you can import them from the standard namespace like so:
1
2
3
using std::cout;
using std::cin;
using std::endl;
Last edited on
Appreciate all the feedback. Thanks!
Topic archived. No new replies allowed.