1

I am working on learning a little about pointers in c++. I get not get my head around the ways in which pointers are declared. The book initializes the pointer declaration by first using an asterisk sign prior to a pointer variable.

int *p;  

After then it is required to initialize a new variable in the heap using the "new" operator and then assign value by dereferencing the pointer.

p = new int;
*p = 45;

Now there are some examples in the book where the writer does all of the work in a single statement using the following code,

int *p = new int;
*p = 45;

It is quite confusing. How can we assign a new int to *p because we have to assign the value to *p, as in the first case?

P.S : I have tested the second case in the compiler and it works like a charm. But i can not get my head around it.

3
  • 1
    It's the same thing as int i = 5;, except instead of int, you have an int *, and instead of 5, you allocate memory and initialize the pointer to the location of that memory. Commented Aug 5, 2013 at 20:56
  • Both of the cases are the same. int *p = new int; is just shorthand for the first example taking one line instead of 2. Commented Aug 5, 2013 at 21:00
  • Don't worry too much about that *. In practice, you have smart pointers like std::shared_ptr<int> p = new int;, and furthermore they appear as class members, and are initialized in the constructor (your C++ course might cover things in a different order, but a good course should not start with int *p. Commented Aug 6, 2013 at 11:34

3 Answers 3

8

you're thinking of it the wrong way.

Think of the * as part of the type identifier.

int* p;

it's not a *p it's a int *

Sign up to request clarification or add additional context in comments.

2 Comments

Keep in mind, the real confusion comes when you try to declare multiple pointers in the same statement. int* a, b, c is incorrect, you will make one pointer a and two non-pointer integers b and c. There needs to be a * assigned to each individual value int *a, *b, *c in order to make them all pointers. So in this case, it is hard to think of their types as int*.
@Lochemage that's true, but what I find interesting is you can actually do typedef int* ptr_t and then declare ptr_t a,b,c and they will all be treated as pointer to int. So in that sense you can sort actually regard int* as a separate type!
6

The real confusion for me when I was learning pointers was the double use of the asterisk (*).

Like Sam I am says in his answer, the asterisk is part of the type identifier in the declaration, like this:

int* p;

Then, its other use is for the dereference operator, which gives you access to the value the pointer is pointing to (in this case to an int). This is the second (or third) line in your code:

*p = 45;

So in order to keep those two meanings apart for yourself, I would advise to use the syntax I used above when declaring a pointer. Like others mentioned, there are actually three ways this can be done:

int* p;
int * p;
int *p;

Or any amount of spaces in between. However, I don't like the middle case as it sort of looks like a multiplication or something. The third case is required when declaring multiple pointers on one line:

int *p, *q;

Because the following would only create one pointer, and then an int:

int* p, q;

But don't worry about declaring multiple pointers on one line. You never really have to use it if you don't want to.

Comments

1

The following declarations are identical as far as the compiler is concerned:

int* x; // alternative [1]
int *x; // alternative [2]

The choice of [1] or [2] is a matter of style, preference, and subject of ongoing religious wars.

When you say int* x or int *x, you have stated that the variable x will store a pointer to an integer. And you can either pass that pointer later on, as in:

int* x;
x = new int;

or immediately, as in:

int* x = new int;

Notice that it would not have mattered if I wrote int *x in either case. Alas, even int * x is valid.

Some say that you should prefer the notation T *x because you can read it as *x is a T (as opposed to reading it as (x is a pointer to T)). Others prefer the notation T* x because it seems to stress the fact that x is of type T* (in other words, pointer to T).

Some would say that the notation T* x can help you avoid the following mistake:

T *x, y, z; // (x is a pointer to T, but y and z are NOT pointers to T; confusing)

in contrast

T* x, y, z; // (x, y, and z are all pointers to T.

So, pick and choose the notation you prefer. However, for the purposes of your question remain with the following one: when you say T* x you are only saying that x is of type pointer to T.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.