difference between int *p and int* p

Hello,

I am self learning C++.
I have 3 questions:

1. what is the difference between int *p and int* p
2.
int *p;
cout << p;.................. (line 2)
getch();
return 0;

I get memory address but when I write on line 2

cout << *p,

I get this answser "1528349827"

3. WHat is the difference between * on a pointer and a referencing asterisk

Thanks you

Last edited on
1. what is the difference between int *p and int* p

To the compiler, not a bit of difference.

To us humans, it can be a crusade of "the right way to do things." I personally prefer int* p;.

To answer 2..... https://www.learncpp.com/cpp-tutorial/67-introduction-to-pointers/ and https://www.learncpp.com/cpp-tutorial/uninitialized-variables-and-undefined-behavior/
(when you created your pointer you didn't assign it any value.)

3. see the first link, introduction to pointers.
1. There is no difference.

Often, C programmers prefer int *thing, but C++ programmers prefer int* thing.

C and C++'s declarator syntax is somewhat suggestive: int *thing is intended to communicate that *thing is an int; double foo() is intended to indicate that foo() is a double.

When it comes to the position of the asterisk, Stroustrup broke C's convention because C++ was designed as a more type-focused language.
http://www.stroustrup.com/bs_faq2.html#whitespace

More discussion here:
http://www.cplusplus.com/forum/general/243869/
Last edited on
The danger with the syntax is in the following:

 
int* x, y;  // Not what it looks like 

Many C++ programmers (and many unsuspecting C programmers) have thought that y was also a pointer, but it is not. It is equivalent to:

1
2
int * x;  // pointer to int
int   y;  // int 

That’s right. y is just an int.

There are two conventions to combat that error. The first is newer, actually, but still older than C++ and alluded to by mbozzi, which says keep them dang asterisks with them variables:

 
int *x, *y;  // both are pointers. yay!  

The other is also older than C++, and these days more common (I think), which says one variable per type per line:

1
2
int * x;  // pointer
int * y;  // pointer 

This smells much cleaner and is definitely easier to read. Spacing around the asterisk no longer matters. The following are all equivalent while maintaining that same readability:

1
2
3
4
int*x;
int* x;
int *x;
int * x;

I personally tend to the left for type names, and to the right for referencing and dereferencing.

1
2
3
int x = 7;
int* y = &x;
int& z = *y;

But in the end it does not matter where the spaces go. Pick your preference for your own work, and use the project guidelines for other people’s work.

Hope this helps.

1
2
int
*                  x;
Duthomhas wrote:
int* x, y

That is something so alien to my way of thinking, I would never write multiple variable declarations on a single line.

My teeth ache just looking at that code snippet.

One Type, One Declaration, One Line!
The difference is my dignity.

My teeth ache just looking at that code snippet... One Type, One Declaration, One Line!

I feel the same way. I'm comfortable looking at it most of the time, but I never code that way.
another notation thing:

*p and p[0] are the same thing.
*p is fine until it isnt.. what if you are doing an equation with pointer variables...

*r = *p * *d; //this gets really bad really quickly for big equations
r[0] = p[0] * d[0]; //ahhh :)

think of pointers like array index.
int x[] = {10,20,30,40};
int px = 1;
cout << px; // 1
cout x[px]; //20

its the same idea.
cout a pointer, you get the address (its like the array index 1 in px above)
cout a pointer with a * or [] dereference, its like x[px] above, its the value at the location in x
its just that x is hidden from you with pointers; its your machine's ram, really.
Ah, don't you just love arrays and pointers!

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

int main()
{
   int A[] = { 10, 20, 30, 40 };
   int *p = A;

   cout << A[2]       << '\n'
        << 2[A]       << '\n'
        << *(p + 2)   << '\n'
        << *(2 + p)   << '\n'
        << *(A + 2)   << '\n'
        << (2 + p)[0] << '\n';
}
Last edited on
To explicitly answer question 3, because I know it can be confusing:

1) When used as part of a declaration or definition, using an asterisk indicates that you're declaring a pointer:

1
2
3
4
5
6
7
int* p1 = nullptr; // Declare a variable whose type is "pointer to an integer"

int *p2 = nullptr; // Declare a variable whose type is "pointer to an integer"

void myFunction1(int* p3);  // Declare a function which takes as its argument, a pointer to an integer.

int* myFunction2();  // Declare a function which returns a pointer to an integer. 


2) When used as a unary operator, the asterisk is the dereference operator. It looks at the address stored in the pointer, and returns the value stored in the memory of that address.

1
2
3
4
5
6
7
8

int  myInt = 3214;
int* p1 = &myInt;  // p1 stores the address of myInt

int myInt2 = *p1 ;  // Looks at the address stored in p1, and gets the value stored at that address, i.e. 3214

std::cout << p1;  // Outputs a hex number that is the address of myInt
std::cout << *p1; // Outputs 3214 


The important thing is understanding from the context, whether the asterisk is specifying a type (as part of a declaration), or whether it's being used as an operator.
Last edited on
One more thing ...
sihaqqi wrote:
1
2
3
int *p;
cout << p;  // I get memory address
cout << *p; // I get this answser "1528349827" 

Note: Your "memory address" is a hexadecimal number. All hexadecimal numbers are not addresses.

Q: What address do you get?
A: The address that you did store in p.

Q: What address did you store in p? In int *p;
A: You didn't. You did not initialize/set p with a value.
The number you get is not a valid address, it is just random garbage.

Q: What is this "1528349827"?
A: You use your random garbage as an address and read/show the values from that location as an integer.


1
2
3
4
int x;
int *p = &x; // make p point to x
cout << p;  // You get the address of variable x
cout << *p; // You get random garbage 

We did initialize p with deterministic value. Not good enough.

Once more:
1
2
3
4
int x = 42;
int *p = &x; // make p point to x
cout << p;  // You get the address of variable x
cout << *p; // You get "42" 

Lesson of the day: do not use uninitialized variables.
Note: Your "memory address" is a hexadecimal number. All hexadecimal numbers are not addresses.

a number is a number, no matter what format you print it in. One *normally* prints addresses in hex by convention.
Topic archived. No new replies allowed.