declare

Pointers and arrays



Sharing buttons:

the concept of pointers and arrays

in C or C++ go together.

there is a very strong relationship between these two concepts

and in this lesson we will be discussing this relationship

when we declare an array let's say we declare

an Integer array A of size 5

then we create 5 integer variables named

A0 A1 A2 A3 ad A4

and now these five integers will be stored in the memory as a block

of

five consecutive integers something like this

what we are showing here in the right if A0 or the first element in the

array

stored at address let's say address 200 and

in a typical compile modern day compiler integer is stored in four bytes

so A1 will be four bytes ahead of A0 at 204

and A2 will be at 208 A3 will be at 212

A4 will be at 216

the overall size of array will be 20 bytes and these 20 bytes

will be one consecutive block we are only showing the section of the memory in

which A is

stored sometimes we also show the memory

horizontally something like this from left to right

we increase the address but these two representations are just

for the sake of understanding let's use the horizontal representation of the

memory

this time I will show this memory a little more extending towards

the right so that I can accommodate a couple of more variables

let's say apart from this integer Array A

I have an integer variable

X and its value is 5 and let's say

X is located at address 300 now

let's say I have a pointer to integer P

and in P I store address of

X if we print P

then the value and P would be 300 so this statement will print

300 and if we dereference P

and try to bring the address stored in, try to print the value stored

in this location P in the value would be

Five this is fine but we also know that we can do

an operation something like increment and decrement

a pointer variable by a constant so we can do something like P equal

the P Plus one and this will take us to the address of the next integer

and because integer is four bytes so now P would be 304

so if I want to print p now then the output should be 304 but

if we try to dereference P now and try to print

* p then we do not know the value at this address

so we cannot say what will be printed it's like

we know that Mr ex lives at address 300 but we do not know

who is his neighbor who lives at address 304

but for this integer Array A lets us say I'm writing this same integer array in the

right here

which is located at address 200 if I declare a pointer to integer

P and store the address of the first element

by putting an ampersand operator in front of A0

then printing P would give us

in this scenario the output 200 and printing

*P would give us but before that lets

say we have these values in the array lets fill up some values in the array

so

*P would be 2 and if i want to print

P plus one then the address would be

204 and if I dereference P+1 and try to bring this value then

print this value it will be Four and similarly if we wanted

the third element in the array we could do a P plus 2

here so using pointer arithmetic make sense in the case of

pointers because in the case arrays because we know what

is in the adjacent location there is one more property of array

if we just use the name of the Array A

then A gives us the pointer to the first element in the array so we can write

a statement like

P is equal to A in fact we do not even need to take this

address in another pointer variable if we simply print A

then this gives us nothing but the address of the first element in the array

and if we want to dereference this and

try to print *A then this will give us the value

so if we want to perform something like

we want to print A plus one then

this will give us the address 204 and *(A+1) will give us the

address of the second element in the array

value of the second element in the array. For an elemen

in the array at index i

we can retrieve the address of this particular element in the memory

using either ampersand A[i]

or simply A plus

i and these two will give us the address

of A[ i] and the value of A[ i] can be

retrieved using either we

simply use A[ i] or we can also use *

A plus i and *(A+1) will also give us the value

now this is an important concept you can write

ampersand A[i] or A plus i for each other and they mean the same

and we can write A[i] or *(A+1) for each other and they mean the same

tha address of the first element in the array

can also be called the Base address and

A simply using the variable name A

gives us the base address of the array let us now

see some code examples and try to solidify our concepts further

in my program lets say we have

and integer array A now as we said

if we simply print A then this should give us the address of the first element

in the array

and we can also get the address of the first element in the array by

using ampersand written in front A[0]

if I simply print A[0] it will print the first element in the array

and we can also print the first element in the array

by using *

operator in front of the variable name A

let us now run this and see what happens

and i also need to put an endline afte r each of these print statements

okay so the output is that the first two lines are the same

they are giving us the address of the first element in array.

and the second lines are giving us the value in fact

if we run a loop like this from 0 to 4

then they can print address of the

element at index i as &A[i] or

(A+i) and we can print the value of ith element as

A[i] or *(A+i) now if we see the output here

this is a fresh run a new run so old address allocations will

change but if we see the address printed in

two lines for A[0] is same the value same again for A1 address is same

four bytes ahead of the previous address and the value is as expected

and we can go on like this

now one more thing even though just using the variable name

A returns us pointer to the

base address or the address of the first element

and we can equate the variable name

A again some pointer variable like this you cannot do something like

A plus plus increment thel value of A this will give us compilation error

we can do something like P++ once we assign

A to some pointer variable, other pointer variable

but incrementing A itself would be in valid

so this was how arrays are stored in memory and how

that addresses can be manipulated and how they can

access the values using pointers

in the next lessons in the coming lessons we will talk about

character arrays and even talk about passing arrays

as function arguments.so thanks fo r watching