Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Pointer Array

C Pointer Array

C Pointer C Pointer

A C pointer array is an array in which each element is a pointer to some data type.

A pointer array stores a group of pointers, and each pointer can point to a different data object.

Pointer arrays are commonly used to handle multiple data objects, such as arrays of strings or arrays of other complex data structures.

Let us look at an example that uses an array of 3 integers:

Example

#include

<

stdio.h

const

int

MAX

=

3

;

int

main

(

)

{

int

var

[

]

=

{

10

,

100

,

200

}

;

int

i

;

for

(

i

=

0

;

i

<

MAX

;

i

++

)

{

printf

(

"

Value of var[%d] = %d

\

n

"

,

i

,

var

[

i

]

)

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Value of var[0] = 10

Value of var[1] = 100

Value of var[2] = 200

There may be a case where we want an array to store pointers to int, char, or other data types.

The following is a declaration of an array of pointers to integers:



int *ptr[MAX];

Here, declare ptr as an array consisting of MAX integer pointers. Therefore, each element in ptr is a pointer to an int value. The following example uses three integers that will be stored in an array of pointers, as shown below:

Example

#include

<

stdio.h

const

int

MAX

=

3

;

int

main

(

)

{

int

var

[

]

=

{

10

,

100

,

200

}

;

int

i

, *

ptr

[

MAX

]

;

for

(

i

=

0

;

i

<

MAX

;

i

++

)

{

ptr

[

i

]

= &

var

[

i

]

;

/*

Assign the address of an integer

*/

}

for

(

i

=

0

;

i

<

MAX

;

i

++

)

{

printf

(

"

Value of var[%d] = %d

\

n

"

,

i

, *

ptr

[

i

]

)

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Value of var[0] = 10

Value of var[1] = 100

Value of var[2] = 200

You can also use an array of pointers to char to store a list of strings, as follows:

Example

#include

<

stdio.h

const

int

MAX

=

4

;

int

main

(

)

{

const

char

names

[

]

=

{

"

Zara Ali

"

,

"

Hina Ali

"

,

"

Nuha Ali

"

,

"

Sara Ali

"

,

}

;

int

i

=

0

;

for

(

i

=

0

;

i

<

MAX

;

i

++

)

{

printf

(

"

Value of names[%d] = %s

\

n

"

,

i

,

names

[

i

]

)

;

}

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Value of names[0] = Zara Ali

Value of names[1] = Hina Ali

Value of names[2] = Nuha Ali

Value of names[3] = Sara Ali

Let us look at another simple example. We first declare a pointer array ptrArray containing three integer pointers, then point these pointers to different integer variables num1, num2, and num3, and finally use the pointer array to access the values of these integer variables.

Example

#include

<

stdio.h

int

main

(

)

{

int

num1

=

10

,

num2

=

20

,

num3

=

30

;

// Declare an integer pointer array containing three pointers

int

ptrArray

[

3

]

;

// Point the pointers to different integer variables

ptrArray

[

0

]

= &

num1

;

ptrArray

[

1

]

= &

num2

;

ptrArray

[

2

]

= &

num3

;

// Use the pointer array to access the values of these integer variables

printf

(

"

Value at index 0: %d

\

n

"

, *

ptrArray

[

0

]

)

;

printf

(

"

Value at index 1: %d

\

n

"

, *

ptrArray

[

1

]

)

;

printf

(

"

Value at index 2: %d

\

n

"

, *

ptrArray

[

2

]

)

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:

Value at index 0: 10

Value at index 1: 20

Value at index 2: 30

Pointer arrays are very useful in C, especially when dealing with data structures with a variable number of elements, such as dynamically allocated string arrays or dynamically created struct arrays.

C Pointer C Pointer