Pointers in C++
Pointers in C++
In C++, pointers are variables that store the memory addresses of other
variables.
Address in C++
Every variable we declare in our program has an associated location in the
memory, which we call the memory address of the variable.
If we have a variable var in our program, &var returns its memory address. For
example,
OUTPUT:
C++ Pointers
Here is how we can declare pointers:
int *ptrVal;
Here, we have declared a variable point_var which is a pointer to an int.
We can also declare pointers in the following way:
int a = 5;
int* ptrVal = &a;
Here, 5 is assigned to the variable a. And the address of a is assigned to
the ptrVal pointer with the code ptrVal = &var.
Note: It is a good practice to initialize pointers as soon as they are declared.
OUTPUT:
Get the Value from the Address Using Pointers
To get the value pointed by a pointer, we use the * operator. For example:
int a = 5;
// assign address of a to ptrVal
int* ptrVal = &a;
// access value pointed by ptrVal
cout << * ptrVal << endl; // Output: 5
In the above code, the address of var is assigned to ptrVal. We have used the
*ptrVal to get the value stored in that address.
When * is used with pointers, it is called the dereference operator. It operates
on a pointer and gives the value pointed by the address stored in the pointer.
That is, * ptrVal = a.
OUTPUT:
OUTPUT:
OUTPUT:
Here ptrVal holds the address of a, and by dereferencing ptrVal with *ptrVal,
we can access and modify the value stored at that address, which in turn
affects the original variable var.
Calling a Function: Call-by-Value
OUTPUT:
Explanation:
From the above code, it is clear that:
• When using the integer pointer to an array, cout prints the base address
of that integer array.
• But when the character pointer is used, cout prints the complete array of
characters (till it encounters a null character) instead of printing the base
address of the character array.
Array Notation vs Pointer Notation
Array Notation: Array notation directly accesses elements using their index. For
example, array[i] retrieves the element at index i. This notation is intuitive and
widely used for working with arrays. Both notations ultimately reference the
same memory location.
Pointer Notation:
Pointer notation accesses elements in an array by using the memory address of
the array and pointer arithmetic. In C++, an array name acts as a pointer to the
first element. For example, *array accesses the first element, and *(array + i)
accesses the element at index i.
Why Use Different Notations?
• Array Notation: This is straightforward and commonly used to access
elements of an array by their index.
• Pointer Notation: This is a more flexible way to traverse and manipulate
arrays in C/C++. It highlights the relationship between arrays and
pointers, which is a fundamental concept in C/C++ programming.
Character Array and Pointer Notation
OUTPUT:
Explanation:
1. Accessing Elements Using Array Notation:
o name[0] refers to the first character in the array, which is 'J'.
o The statement cout << name[0] << endl; prints the first character
of the string, which is 'J'.
2. Accessing Elements Using Pointer Notation:
o In C++, an array name (e.g., name) is treated as a pointer to the
first element of the array.
o *name is pointer notation that dereferences the pointer name and
accesses the value at the first memory location, which is 'J'.
Therefore, cout << *name << endl; also prints 'J'.
o *(name + 0) is another way to access the first element using
pointer arithmetic. Here, name + 0 points to the first character of
the string, and dereferencing it with * gives the character 'J'. The
statement cout << *(name+0) << endl; also prints 'J'.
Summary of Outputs:
• name[0]: Accesses the first element of the array using array notation.
• *name: Accesses the first element of the array using pointer notation.
• *(name + 0): Another way of accessing the first element using pointer
arithmetic.
Pointer to a String
This program demonstrates how to access characters in a string using both
array and pointer notation. The string "Jessy Smith" is assigned to a pointer
variable, and then different ways of accessing its first character are shown.
Key Concepts:
1. Pointer to a String:
o char *name = "Jessy Smith"; initializes a pointer name to point to
the first character of the string "Jessy Smith". In C++, string literals
are stored as arrays of characters, and the pointer points to the
first character, 'J'.
2. Accessing the First Character (Array Notation):
o name[0] accesses the first character of the string ('J') using array
notation, where name is treated as an array of characters.
3. Accessing the First Character (Pointer Notation):
o *name accesses the first character of the string using pointer
notation. Since name points to the first character ('J'),
dereferencing it with * gives 'J'.
4. Pointer Arithmetic (Pointer Notation):
o *(name + 0) is another way to access the first character. It adds 0
to the memory address stored in name, resulting in the same
address, and dereferencing it with * gives 'J'. This is equivalent to
name[0].
Output:
All three statements print the first character of the string, which is 'J'.
Summary:
• name[0], *name, and *(name + 0) all access the first character of the
string "Jessy Smith" using different notations.
• The array and pointer notations are interchangeable in this case because
arrays and pointers are closely related in C/C++.
Pointers and Arrays
An array is a series of elements of the same data type Pointers can be used to
manipulate arrays rather than using an index.
The name of an array points to the first element of the array.
Relationship between Pointers and Arrays
Array Name as a Pointer: The name of an array acts as a pointer to the first
element of the array.