-2

My code is

int null = 0;
int *p = null;

It gives me an error: cannot initialize a variable of type 'int *' with an lvalue of type 'int'.

In C it would work.

EDIT:

How can I make it work in this spirit?

It's from an exercise (2.32) from the Primer C++ book:

Is the following code legal or not? If not, how might you make it legal?

int null = 0, *p = null;
gon1332
  • 1,930
  • 1
  • 24
  • 30

4 Answers4

1

change int *p = null; to int *p = (int*)null;

Shang
  • 518
  • 3
  • 13
  • I've done this from the first time but a get a warning: cast to 'int *' from smaller integer type 'int' [-Wint-to-pointer-cast] – gon1332 Apr 24 '14 at 23:55
  • That warning suggests you are compiling for 64-bit, where a pointer is 8 bytes in size but an `int` is 4 bytes in size. – Remy Lebeau Apr 24 '14 at 23:56
  • I was wondering if there would be something else that could do the job, just for the experience of playing with C++, especially from the time that the book hasn't even mention typecasting yet. I'm with you guys but is there something that I miss? – gon1332 Apr 25 '14 at 00:09
  • @gon1332: What job, exactly, are you trying to do? Convert an int to a pointer? The book is not asking you to do that. It's just asking you how to fix the code. And the correct way to fix the code is to change the second `null` to `nullptr`, or perhaps `&null` (so p points to the int `null`). If you want to convert an int to a pointer though, you need a cast. – Benjamin Lindley Apr 25 '14 at 00:19
  • Yes, you're right. The answer is to initialize with nullptr or with the literal 0. – gon1332 Apr 25 '14 at 00:24
  • Use `(long*)null` instead of `(int*)null`. It should eliminate warning. – Osmin Apr 25 '14 at 10:05
1

If you are using C++11 then use nullptr:

int *p = nullptr;

If you are not using C++11 then use NULL instead:

int *p = NULL;

Do not define your own null value at all, since C/C++ already has one of its own. NULL has no type, it is simply defined as 0, and an integer literal 0 can be assigned to a pointer:

int *p = 0;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
0

You should use nullptr in C++11. C++ doesn't do automatic conversion between int and x* (where x is type), so using an int typed variable to define null won't work. In pre-C++11, you can use integral literals, like 0. (You can still use integral constants after the new standard, but there are good reasons for using nullptr over NULL. What are the advantages of using nullptr?)

You can read more about it nullptr here: http://en.cppreference.com/w/cpp/language/nullptr.

Community
  • 1
  • 1
Tyler
  • 1,818
  • 2
  • 13
  • 22
  • how could I make the above work as it is, without nullptr? I'm new in C++. – gon1332 Apr 24 '14 at 23:53
  • 2
    If you are using C++11 then use `nullptr`: `int *p = nullptr;`. If you are not using C++11 then use `NULL` instead: `int *p = NULL;`. Do not define your own `null` value at all, C/C++ already has one. `NULL` has no type, it is simply defined as `0`, and an integer literal `0` can be assigned to a pointer: `int *p = 0;` – Remy Lebeau Apr 24 '14 at 23:54
  • I'm now learning C++ and I read the Primer C++. It is one excersise (2.32). The solution that you suggested is the one that came first. I was wondering if there would be something else that could do the job, just for the experience of playing with C++. – gon1332 Apr 25 '14 at 00:00
  • @gon1332: Any "null pointer constant" would work, which includes `false` and `0L`. But that's obscure and obsolete; use `nullptr`. It's safer. – MSalters Apr 25 '14 at 00:47
  • @gon1332 You don't have to select answers if they don't answer the question you asked. This one doesn't (it doesn't explain why `int* p = 0;` works, and seems to claim `NULL` wouldn't work, which is wrong). By selecting it, you make it less likely that someone else will come and actually answer your question. – juanchopanza Apr 25 '14 at 05:59
  • The correct answer is that with `nullptr` or `NULL`. @RemyLebeau answered it correctly. If he posts I'll accept him. You're right about `NULL`. Both `nullptr` and `NULL` will work. – gon1332 Apr 25 '14 at 07:12
-2

I think *p is not a correct identifier for C. this is because a good identifier shouldn't start with a symbol but a letter.

Klaus By
  • 9
  • 1