Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ch2/2.32.cpp #3

Open
AlesaW opened this issue Dec 4, 2017 · 4 comments
Open

ch2/2.32.cpp #3

AlesaW opened this issue Dec 4, 2017 · 4 comments

Comments

@AlesaW
Copy link

AlesaW commented Dec 4, 2017

// Method 1
const int null1 = 0;
int *p1 = null1;

// Method 5
constexpr int null2 = 0;
int *p5 = null2;

are invalid, leads to type conversion error.

@simonZ0919
Copy link

// pointer is supposed to store the address, here is a simple correction
int null = 0;
int *p = & null;

@superluminance
Copy link

superluminance commented Sep 7, 2019

@AlesaW 1 & 5 compile w/ visual studio compiler, but not in GCC.
@simonZ0919 Unfortunately your solution is not a correction.

@ColinM9991
Copy link

ColinM9991 commented Feb 18, 2022

Tested this locally. 1 & 5 do not compile with MSVC.

As @simonZ0919 says, a pointer stores an address. The book itself gives the answer prior to this exercise section.

The following is correct.

int null = 0;
int *p = &null;

image

@lthiet
Copy link

lthiet commented Apr 10, 2022

Since this exercise was left in a section that talked about constexpr, I think the intention of the authors were to make use of constexpr to make this expression valid. I would not say that @simonZ0919 solution serves the purpose of learning about constexpr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants