Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About Initializing Pointers in C++ [duplicate]

Tags:

c++

pointers

I've been reading C++ Primer 5th edition. It mentions that

It is illegal to assign an int variable to a pointer, even if the variable’s value happens to be 0.

I give it a try, and find the following result:

int *u = 0; // success

int *w = 123; // fail 
/* compile error:
ptr_test.cc:9:12: error: invalid conversion from 'int' to 'int*' [-fpermissive]
    int *w = 123;
*/


int zero = 0;
int *v = zero; // fail
/* compile error
ptr_test.cc:9:12: error: invalid conversion from 'int' to 'int*' [-fpermissive]
    int *w = zero;
*/

Can someone help me explain the exact rule of pointer initialization? Why assigning to a int pointer to 0 is fine, but 123 is not ok, though they are both integer? Why does 123 result in a to "*int" cast, while 0 does not?

BTW, I'm using g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0.

like image 903
Okd Avatar asked Jan 04 '20 06:01

Okd


People also ask

What is the initialization of pointer?

The initializer is an = (equal sign) followed by the expression that represents the address that the pointer is to contain. The following example defines the variables time and speed as having type double and amount as having type pointer to a double .

Can we initialize a pointer in C?

So 'a' is an integer variable and by doing &a, we are getting the location where a is stored in the memory and then letting the pointer point to that location. This is the first method of initializing a pointer in C.

Do pointers need to be initialized?

All pointers, when they are created, should be initialized to some value, even if it is only zero. A pointer whose value is zero is called a null pointer. Practice safe programming: Initialize your pointers!

What are the two types of initialization?

Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.


2 Answers

Why assigning to a int pointer to 0 is fine

Because 0, besides being an integer literal, is also a null pointer constant. And a null pointer constant converts to any pointer type.

but 123 is not ok

Because 123, or any other integer literal besides those whose value is 0, is not a null pointer constant. And integer expressions (except for null pointer constants) don't implicitly convert to pointer types.

Why does 123 result in a to "*int" cast, while 0 does not?

Neither "results in a cast". Cast is an explicit conversion, and in these examples all conversions are implicit. Implicit conversion from integer to pointer is ill-formed, which is why you get the error. The null pointer conversion is implicit and well-formed, which is why you don't get an error.

Can someone help me explain the exact rule

Here is the exact rule (quote from the latest C++ standard draft):

[conv.ptr]

A null pointer constant is an integer literal ([lex.icon]) with value zero or a prvalue of type std​::​nullptr_­t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type ([basic.compound]) and is distinguishable from every other value of object pointer or function pointer type. Such a conversion is called a null pointer conversion. ...

P.S. The keyword nullptr is a prvalue of type std​::​nullptr_­t. It should always be favoured over using literal 0 except in the case you intend to support pre-C++11 compilers.

like image 107
eerorika Avatar answered Oct 07 '22 06:10

eerorika


C++ has a special case for the literal integer 0, which is implicitly convertible to a null pointer.

like image 14
Some programmer dude Avatar answered Oct 07 '22 05:10

Some programmer dude