0

I know that * is widely used as the multiplication operator, it can be used for so much more though. For example:

int number = 5;                 
int* pointer = &number;

In this example a int pointer gets declared and defined with the * syntax to the RAM address value of the variable number. Now if we'd want to display the value of RAM address of the variable "number" we would have to use the "*" again. Like this:

int pointer_value = *pointer;

Now we used the * syntax again but in the definition of the int variable "pointer_value". This will display the value of RAM address pointing to the value of the variable "number".

Now my question is: What is the "*" mostly used for, I saw so many examples like:

const char* word;

etc. etc. The many opportunities the "*" can be used for confuse me. Can somebody explain?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Lotus
  • 119
  • 2
  • 8
  • 1
    Depends on where you use it; in the example of `int pointer_value = *pointer;` the `*` is used to ***dereference*** a pointer. In short, context matters. – Elliott Frisch Oct 24 '20 at 15:26

2 Answers2

3

Grammatically, the declaration is int *pointer, not int* pointer, and the * conveys essentially the same meaning both in a declaration and in an expression.

In an expression, *pointer refers to some object using its memory address.

A declaration such as int *pointer means that *pointer will refer to an int. Since *pointer refers to an int, this means that pointer is a pointer to an int. This is explained further here and here.

In expressions, when * appears as a unary operator (with an operand immediately after it and no operand immediately before it), it is the indirection operator that means to use the operand as an address to refer to the object at that address.

When * appears as a binary operator (with an operand immediately before it and an operand immediately after it), it is the multiplication operator.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

This "*" is asterisk,if you want to print value of num you print *pointer. if you want print to address then you print pointer or &num print the address of num. If you want to print the address pointer you print &pointer.