-4

what does the * mean in

int* p = nullptr;

Also, it would be helpful if anyone can example what nullptr means. Is it equivalent to null? Sorry, I recently started learning c++;

Henry
  • 13
  • 1
  • 1
  • `int *` here declares a pointer-to-an-int type. So `p` will be a pointer to an int. – ash Jul 31 '21 at 04:49
  • 2
    `nullptr` is a constant that is pretty much the same thing as NULL. – ash Jul 31 '21 at 04:50
  • 6
    In that case I would recommend our curated list of [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Quimby Jul 31 '21 at 04:52
  • If I said "nullptr" is a null pointer, would that make sense to you or not? – Wyck Jul 31 '21 at 04:55
  • 5
    Definitely get a few good books. It'll speed your learning time enormously. C++ is a hard language to master without a good guide, and it doesn't reward trial and error very often. – user4581301 Jul 31 '21 at 04:56
  • Partial duplicate of [What exactly is nullptr?](https://stackoverflow.com/questions/1282295/what-exactly-is-nullptr) – user4581301 Jul 31 '21 at 05:04
  • SO isn't a good place for learning like this. Please find a good book and learn from it – phuclv Jul 31 '21 at 05:37

3 Answers3

2

In C and C++, it will be easy if you consider * as "value at address" operator. Its counterpart & is "address of" operator. Now let's see how to interpret this:

int* p = nullptr;

One can read this as "Value at address stored in p is an int and that address is initialized with nullptr".

Before C++/11, we used to assign 0 or NULL to it. NULL is also defined as 0. But it could be confused with number 0 that is why nullptr was introduced. It is equivalent of null in other languages. For details See: nullptr

  • A good example of how the problem discussed here lingers is `std::string temp(0);`, sometime used by folk trying into convert an integer to a string. If you use any number other than zero, it won't compile. But with 0 it compiles and crashes the program because the compiler interpreted the 0 as a pointer to a null-terminated character array at address 0 and calls the `const char *` `string` constructor. Really sucks. – user4581301 Jul 31 '21 at 05:15
  • That is related but a complete different topic in itself. Let me give you another even more interesting example. `int arr[3]={1,2,3};` and then try to print `1[arr]`. But these are just wordplay once you get a grasp over language. I think C/C++ should not be taken as other RAD languages. One should study it before jumping to start working on it. – Ashutosh Raghuwanshi Jul 31 '21 at 05:23
  • 2
    More fun/on topic example: `NULL[arr]` -> https://ideone.com/suEh5H – user4581301 Jul 31 '21 at 05:28
  • 2
    In my experience, there's a lot of confusion from novices out there when they think of `int*` as having an operator `*` in it. It's looked best to fully differentiate `*` and `&` when used in types vs. their use as operators in expressions rather than conflate the two and leave novices to not realize that `*` has two completely different meanings that are both related to pointers. (Aside, as C declaration syntax is modelled after expressions, it makes sense to use the expression counterparts _for reading declarations_, but not for explaining how to use pointers in general.) – chris Jul 31 '21 at 05:35
  • @chris yes, some people might find that approach simpler but that just because different people understand things differently. – Ashutosh Raghuwanshi Jul 31 '21 at 05:40
2

what does the * mean in

int* p = nullptr;

* refers to a pointer-to-an object which holds a specific location/address in memory, in your case it is a pointer-to-an-int and so it refers to the address of an integer.

Also, it would be helpful if anyone can example what nullptr means. Is it equivalent to null? Sorry, I recently started learning c++;

Both NULL and nullptr point to the zeroth address (0x000000) in memory.

In most cases, the C++11 keyword nullptr is similar to NULL in the sense that they usually point to the same thing. But, there are some subtle differences between the two:

decltype(NULL);    /* 'void*' or `intptr_t`, depends on implementation, the former is
                    * an object pointer type while the latter is just an integer that
                    * can hold an address */
decltype(nullptr); // Always 'std::nullptr_t'

From this, we find that nullptr is in fact, not a pointer but an instance of a class, std::nullptr_t.

Essentially, std::nullptr_t is a wrapper class which indirectly refers to the zeroth address in memory. All instances of this class (including nullptr) are implicitly convertible to the pointer of any type.

The reason for this sort of design was to facilitate function overloading, so that nullptr could have a consistent type that could be safely overloaded, unlike NULL which does not have a consistent type across different implementations:

#include <iostream>
#include <cstddef>

void f(long int) {
    std::cout << "Encountered a long" << std::endl;
}

void f(decltype(NULL)) {
    std::cout << "Encountered NULL" << std::endl;
}

void f(decltype(nullptr)) {
    std::cout << "Encountered nullptr" << std::endl;
}

int main() {
    f(0l);
    f(NULL);
    f(nullptr);
}

In this case, normally, one would think that this would output:

Encountered a long
Encountered NULL
Encountered nullptr

But it gets more complicated than this, on certain implementations, the compiler would give this error:

// Output from the GCC compiler:
source>:8:6: error: redefinition of 'void f(long int)'
    8 | void f(decltype(NULL)) {
      |      ^

This is because decltype(NULL) is long int in this case (not always), which causes the overload void f(long int) to be defined twice, leading to a compilation error.

This is one of the use cases of nullptr. It safely retains its type, which is std::nullptr_t.

In contrast, NULL doesn't have any defined universal type, so it can't be safely overloaded in a function based on its type.

Ruks
  • 3,886
  • 1
  • 10
  • 22
-3

This and this should be helpful

It's basically used to indicate an object handle, interior pointer, or native pointer type does not point to an object.

int* p = nullptr means the integer pointer that doesn't point to an integer variable

  • No, I added another link from gfg also – BestCodingExpert Jul 31 '21 at 05:00
  • 4
    All to often a link to gfg earns scorn because the site has limited, if any, curation. And when the first code example starts with a pair lines famed for their ability to produce whacky errors ([Why should I not #include ?](https://stackoverflow.com/questions/31816095) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721)) we're off to a really bad start. Fortunately, once it gets past that opening bit of idiocy, it's not too bad. No downvote today. – user4581301 Jul 31 '21 at 05:11