0% found this document useful (0 votes)
26 views2 pages

Pointers and Arrays

This document discusses the relationship between pointers and arrays in C++. It explains that arrays implicitly convert to pointers, so an array name can be used like a pointer to the first element. Both pointers and arrays support the same operations. The main difference is that pointers can be assigned new addresses, while array addresses cannot change. The document also notes that array brackets [] are a dereferencing operator like *, but also add an offset to the address. So expressions like a[5] and *(a+5) are equivalent for both pointers and arrays.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Pointers and Arrays

This document discusses the relationship between pointers and arrays in C++. It explains that arrays implicitly convert to pointers, so an array name can be used like a pointer to the first element. Both pointers and arrays support the same operations. The main difference is that pointers can be assigned new addresses, while array addresses cannot change. The document also notes that array brackets [] are a dereferencing operator like *, but also add an offset to the address. So expressions like a[5] and *(a+5) are equivalent for both pointers and arrays.

Uploaded by

icul1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Pointers and arrays

The concept of arrays is related to that of pointers. In fact, arrays work very much like pointers to
their first elements, and, actually, an array can always be implicitly converted to the pointer of
the proper type. For example, consider these two declarations:
1
2
int myarray [20];
int * mypointer;


The following assignment operation would be valid:
mypointer = myarray;


After that, mypointer and myarray would be equivalent and would have very similar properties.
The main difference being that mypointer can be assigned a different address,
whereas myarray can never be assigned anything, and will always represent the same block of
20 elements of type int. Therefore, the following assignment would not be valid:
myarray = mypointer;


Let's see an example that mixes arrays and pointers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// more pointers
#include <iostream>
using namespace std;

int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
10, 20, 30, 40, 50,


Pointers and arrays support the same set of operations, with the same meaning for both. The
main difference being that pointers can be assigned new addresses, while arrays cannot.

In the chapter about arrays, brackets ([]) were explained as specifying the index of an element of
the array. Well, in fact these brackets are a dereferencing operator known as offset operator.
They dereference the variable they follow just as * does, but they also add the number between
brackets to the address being dereferenced. For example:
1
2
a[5] = 0; // a [offset of 5] = 0
*(a+5) = 0; // pointed by (a+5) = 0


These two expressions are equivalent and valid, not only if a is a pointer, but also if a is an array.
Remember that if an array, its name can be used just like a pointer to its first element.

You might also like