What exactly is the purpose of the (asterisk) in pointers?

c++ pointers
c asterisk after variable
c++ pointers and references
pointer to pointer c++
pointer to function c++
c++ double asterisk
pointer with two asterisk
c++ ampersand

I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'.


int main()
{
    int x = 5;
    int *pointerToInteger = & x;
    cout<<pointerToInteger;

}

Why is it that when I cout << pointerToInteger; the output is a hexdecimal value, BUT when I use cout << *pointerToInteger; the output is 5 ( x=5).

* has different meaning depending on the context.

  1. Declaration of a pointer

    int* ap;  // It defines ap to be a pointer to an int.
    
    void foo(int* p); // Declares function foo.
                      // foo expects a pointer to an int as an argument.
    
  2. Dereference a pointer in an expression.

    int i = 0;
    int* ap = &i;   // ap points to i
    *ap = 10;       // Indirectly sets the value of i to 10
    
  3. A multiplication operator.

    int i = 10*20; // Needs no explanation.
    

Why does C use the asterisk for pointers?, At this point, the question is as uninteresting as "why does python 3 use . to was actually the word NEQV (see The BCPL Reference Manual). The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer. Take a look at some of the valid pointer declarations −

C For Dummies Pointer Cheat Sheet, The asterisk is the unary * operator. It is not the * multiplication operator. Pointers must be initialized before they can be used. Initialize a pointer by assigning it to a​  & means the address-of, you will see that in placeholders for functions to modify the parameter variable as in C, parameter variables are passed by value, using the ampersand means to pass by reference. * means the dereference of a pointer variable, meaning to get the value of that pointer variable.

One way to look at it, is that the variable in your source/code, say

int a=0;

Makes the 'int a' refer to a value in memory, 0. If we make a new variable, this time a (potentially smaller) "int pointer", int *, and have it point to the &a (address of a)

int*p_a=&a; //(`p_a` meaning pointer to `a` see Hungarian notation)

Hungarian notation wiki

we get p_a that points to what the value &a is. Your talking about what is at the address of a now tho, and the *p_a is a pointer to whatever is at the &a (address of a).

This has uses when you want to modify a value in memory, without creating a duplicate container.

p_a itself has a footprint in memory however (potentially smaller than a itself) and when you cout<<p_a<<endl; you will write whatever the pointer address is, not whats there. *p_a however will be &a.

p_a is normally smaller than a itself, since its just a pointer to memory and not the value itself. Does that make sense? A vector of pointers will be easier to manage than a vector of values, but they will do the same thing in many regards.

6.7, When declaring a function, put the asterisk of a pointer return value next to the So there are actually a surprising number of uses for pointers. in C++, the following means "allocate memory for an int pointer": So, the asterisk is part of the variable type; without it, that would mean "allocate memory for an int". Then, wouldn't it make more sense if the following meant "allocate memory for two int pointers"?

Pointers - C++ Tutorials, Pointers are said to "point to" the variable whose address they store. Therefore, although these three example variables are all of them pointers, they actually Note that the asterisk ( * ) used when declaring a pointer only means that it is a  a is variable of type "pointer to int", why the hell would we put a asterisk on the other side of a space? It has nothing to do with name, it refers to TYPE, a is of "POINTER TO INT" type. I asked my teacher exact same question 10 years ago, that OP wrote, what the hell is asterisk before function name, what does it have to do with function name?

C Language, c documentation: Same Asterisk, Different Meanings. pointer syntax in C and C++ is that there are actually two different meanings that The asterisk ( * ) has two distinct meanings within C in relation to pointers, depending on where it's used. Language · GNU/Linux · Objective-C Language · opencv · Python Language. A pointer can point to any memory address, and not only to the address of some variable that you have previously declared. However, you must be careful when using pointers in order to get or set the value located at the pointed memory address. For example: int* a = (int*)0x8000000; Here, we have variable a pointing to memory address 0x8000000.

How to Use Pointers in C++ Programming, A pointer is actually a variable that points to an address in memory. It isn't To create a pointer variable, we need an asterisk (*) in front of the name as shown in following You will still need that, because a pointer MUST point to an address. In some programming languages such as the C, C++, and Go programming languages, the asterisk is used to dereference or to declare a pointer variable. In the Common Lisp programming language, the names of global variables are conventionally set off with asterisks, *LIKE-THIS* .

Where should I put the asterisk for pointers in C++? Is it int* ptr, int , While the placement of the asterisk in a pointer variable definition, from the compiler's point of view, is a matter of style, There actually are simple s What are some examples of late binding in C++ other than by using function pointers? I'm here again. While digging in some C source code I found that fragment of code. char **words I know that single asterisk before variable name "points" to pointer, but what is the purpose of those two asterisks ?

Comments
  • One is the pointer (i.e. memory address) - this is the hex value. The other is the contents of that address.
  • * in front of a pointer means to "de-reference" the pointer, or traverse it. * after a type indicates a pointer to the type. A pointer is just a direction to somewhere in memory where a given piece of information is stored.
  • cout << pointerToIntegerprints the address that pointerToInteger points to; cout << *pointerToInteger prints the contents stored at that address. Think of it this way: pointerToInteger is the mailbox in front of your home; *pointerToInteger is what's inside that mailbox.
  • Is there a difference between int* ap and int *ap (other than style)?
  • @c.berger, no, there isn't.