You are on page 1of 3

Pointers Reading Material 2

Pointer and Arrays


The name of the array represents the address of the starting
element of the array.
E.g.
int x [5] = {1, 5, 8, 9, 3};
int * p;
p = x;
Here p is the pointer to the first element of the array which is
same as &x[0].

Pointer and 2D Arrays


For a 2D array all the elements are stored in continuous
location.
That means that all the elements of all the rows are stored
continuously and all the rows are also stored continuously.
E.g.
int a[3][4];
The rows a[0], a[1] and a[2] are stored continuously. And the
elements a[0][0], a[0][1], a[0][2], a[0][3] are also stored
continuously.

Pointers and Character Arrays


Strings are treated as character arrays and they are declared
and initialized as follows:
char str [5] = good;
The compiler automatically inserts the null character \0 at
the end of the string.
C supports another method of creating string using pointer
variables of type char.
E.g. char *str = good;

This creates a string for the literal (i.e. allocates memory and
stores the word good) and then stores its address in the
pointer variable str.
In C constant character strings represents pointer to
themselves.
For e.g. Delhi is a pointer to itself.
So we can use the following
char * string1;
string1 = good;
The second statement is not a string copy operation but
since the constant word good is a pointer to itself so the
pointer string 1 is given the address of good.
To print the string just use:
printf (%s, string1);

Array of Pointers
E.g.

char name[3][25];
char *name[3] = {New Zealand,
Australia, India};
Over here a pointer array is declared in which 3 pointers can
be stored.
So as we already know that the constant strings are pointers
to themselves, so the above assignment is same as
name[0] = New Zealand;
name[1] = Australia;
name[2] = India;
Here the memory allocated is the sum of the 3 strings not
the 3*25, because the computer already know the size of the
strings you are storing so it allocates only that much
memory.

For more you can refer to the following links:


1. Pointers and Arrays :
https://www.youtube.com/watch?
v=ASVB8KAFypk&index=6&list=PL2_aWCzGMAwLZp6LMUKI3cc7p
gGsasm2_

2. Pointer and Character strings


(You can leave the function part: After 11.20)
https://www.youtube.com/watch?
v=Bf8a6IC1dE8&list=PL2_aWCzGMAwLZp6LMUKI3cc7pgGsasm2_
&index=8

3. Array of Pointers
https://www.youtube.com/watch?
v=sHcnvZA2u88&index=10&list=PL2_aWCzGMAwLZp6LMUKI3cc7
pgGsasm2_

You might also like