The Use of * and & in C/C++
Introduction
- C++ is a superset of C. Almost all valid C programs are valid C++ programs. It is thus possible to write a C++ program which uses nothing in C++ which is not in C, although the use of cout or of references (see below) produces a C++ program. C++ and C occasionally differ in style, e.g. to declare p as a pointer to an int variable C uses int *p whereas C++ uses int* p (the compiler doesn't care which you use).
- In C/C++ the characters * and & are used in a variety of ways. The meaning has to be inferred from the context.
The use of * in C
- Multiplication: x = y*z;
- Multiply-assign: x *= y; Means the same as: x = x*y;
- Comments: /* your comment here */
- Pointer declarations: int *p; or int* p; Read: p is a pointer to an int.
- Compound pointers: int **p; or int** p; Read: p is a pointer to a pointer to an int. (Also int ***p; and so on.)
- De-referencing: x = *p; Read: Assign to x the value pointed to by p.
The use of & in C
- Logical-and: if ( ( a>1 ) && (b<0) ) ...
- Bitwise-and: x = a&b; Corresponding bits are and'ed (e.g. 0&1 -> 0)
- Bitwise-and-assign: x &= y; Means the same as: x = x&y;
- Address-of operator: p = &x; Read: Assign to p (a pointer) the address of x.
- The additional use of & (in parameters) in C++
C++ uses a type of variable called a "reference" variable (or simply a "reference") which is not available in C (although the same effect can be achieved using pointers).
References, pointers and addresses are closely related concepts. Addresses are addresses in computer memory (typically the address in memory where the value of some variable is stored), e.g. (in hexadecimal) 0xAB32C2. Pointers are variables which hold addresses, and so "point to" memory locations (and thus to the values of variables). Conceptually, reference variables are basically pointers by another name (but may not be instantiated as such by the compiler).
It is possible to declare a reference within a function, like other variables, e.g.
void main(void)
{
int i;
int& r = i;
...
}
but this is pointless, since the use of the reference is equivalent to the use of the variable it references.
References are designed to be used as parameters (arguments) to functions, e.g.
void f(int& r);
void main(void)
{
int i=3;
f(i);
printf("%d",i);
}
void f(int& r)
{
r = 2*r;
}
This program prints "6" (2*r doubles the value of the variable referenced by r, namely, i).
We could do the same in C by declaring f() as void f(int *r), in which case r is a pointer to an int, then calling f() with argument &i (address-of i), and using de-referencing of r within f(), but clearly C++ provides a more elegant way of passing values to functions (by reference) and returning (perhaps multiple) values from functions (without use of a return statement).
Read More Articles
- Use of getch(),getche() and getchar() in C
- Switch Case Statement Example Program In C Programming Language
- C Character Set
- Convert a Floating-point value to an Integer in C
- Data Input and Output gets and puts Example Program In C
- Special Operators In C
- Pointer Representation and Pointer Example Programs
- C Data Input and Data Output
- Simple While Loop Example Program In C Programming Language
- Data Output printf and putchar Example Program In C
- C Introduction
- C Operators
- Storage Classes In C
- C Pointers
- File Management
- C Identifiers
- Loop Control Statements
- Hello World - Simple C Program
- C Array
- Single Character Output Function : putchar()
- C Reserve Words
- C Specific Properties and Implementation
- If else Statement Example Program In C Programming Language
- If Statement Example Program In C Programming Language
- Confusing Array in C ( Array Representation and Initialization )