Most of the beginners to the C programming language struggle with the symbol *
. We are here to learn about this notation in a very simple way.
The symbol *
is used mainly in three places:
In an arithmetics expression
During the declaration of a pointer variable
In the expressions involving pointer variables.
Hence, there are three names for this symbol in C:
If the symbol *
is used in arithmetic expressions (between two normal variables ), we call it a Multiplication Operator.
Here is a simple code example:
int main() {
int a,b,c;
a = 10;
b = 5;
c = a*b; // Here * is a Multiplication operator
printf("value of c = %d",c);
return 0;
}
Output:
value of c = 50
If the symbol *
is used during the declaration of a pointer variable then we call it an Indirection Operator. The symbol *
directs the compiler during lexical analysis phase that the variable name after this is for a pointer variable, not for a normal variable.
int main() {
int a,b,c;
int *p; // Here * is an Indirection operator
p = &c;
a = 10;
b = 5;
c = a*b; // Here * is a Multiplication operator
printf("value of c = %d",c);
return 0;
}
Output:
value of c = 50
If the symbol *
is used in the expressions before pointer variables then we call it a Value at Operator.
int main() {
int a,b,c;
int *p; // Here * is an Indirection operator
p = &c;
a = 10;
b = 5;
c = a*b; // Here * is a Multiplication operator
printf("value of c = %d",*p); // Here * is a Value at operator
return 0;
}
Output:
value of c = 50
p
is a pointer variable which is holding the address of an integer variable c
. Let's say the address of c
is "1001". Hence the meaning of the expression *p
will be Value at p = Value at 1001 = 50 (i.e, the value of variable c
)
Here is another simple code example:
int main() {
int a,b,c,d;
int *p; // Here * is an Indirection operator
p = &c;
a = 10;
b = 5;
c = 2;
d = *p+a*b // Here first * is Value at operator and second * is a Multiplication operator
printf("value of d = %d", d);
return 0;
}
Output:
value of d = 52
In the above code, p
is a pointer variable which is holding the address of an integer variable c
. Let's say the address of c is "1001". Hence the meaning of the expression *p+a*b
will be as below:
d = *p+a*b
d = value at p + a multiplication b
d = value at 1001 + 10 multiplication 5
d = 2 + 50
d = 52
*
together?Well, here is the final and a little complex example to understand this.
int main() {
int a,b,c,d;
int *p,*q; // Here * is an Indirection operator
int **r; // Here ** is the indrection operator for declaration of a double pointer (pointer to pointer)
p = &c;
q = &a;
r = &p;
a = 10;
b = 5;
c = 2;
d = **r + *q * b;
printf("value of d = %d", d);
return 0;
}
Output:
value of d = 52
Let's assume addresses of variables a
and c
are 1001 and 2002 respectively and the address of pointer variable p
is 3003. We can solve the below expression as:
d = **r + *q * b
d = value at (value at r)) + value at (q) multiplication (b)
d = value at (value at 3003) + value at (1001) multiplication b
d = value at (2002) + 10 multiplication 5
d = 2 + 50
d = 52
Thanks for reading. Hope you understood everything about the mystery!
SHARE YOUR THOUGHTS WITH US!