0% found this document useful (0 votes)
38 views10 pages

Initialization of Pointer Arrays in C

Uploaded by

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

Initialization of Pointer Arrays in C

Uploaded by

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

6/16/24, 1:16 PM Initialization of Pointer Arrays in C

Initialization of
C - Anonymous Structure
and Union Pointer Arrays in C
C - Unions A pointer in C is a variable that
stores the address of another
C - Bit Fields variable. The name of the pointer
variable must be prefixed by the "*"
symbol. Just as in the case of a
C - Typedef
normal variable, we can also declare
an "array of pointers", where each
C - Input & Output
subscript of the array holds the
address of an array type.
C - File I/O

C - Preprocessors Initializing an Array of Pointers


A pointer variable can be initialized at
C - Pragmas
the time of declaration, by assigning
it the address of an existing variable.
C - Preprocessor The following snippet shows how you
Operators can initialize a pointer −

C - Macros int x = 10;


int *y = &x;
C - Header Files
By default, all the variables including
C - Type Casting the pointer variables belong to the
"auto storage class". It means that a
C - Error Handling pointer variable will store an
unpredictable, garbage, random
memory address, which can lead to
C - Variable Arguments
undefined behavior and potential
risks to a program, such as
C - Memory
segmentation fault errors. Hence, it
Management
should be initialized to NULL if we
don’t have a specific value to store at
C - Command Line the time of declaration.
Arguments
int *ptr = NULL;

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 1/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

A "pointer array" stores the address


in each element. The type of the
array must match with the type of
the target variable.

Example 1: Using the "static"


Keyword

You can also use the "static" keyword


to initialize an array of pointers to
store "0" in each subscript −

#include <stdio.h>

int main(){

static int *ptr[5];

for (int i = 0; i < 5; i++){


printf("ptr[%d] = %d\n", i, ptr[i]);
}

return 0;
}

Output

Run the code and check its output −

ptr[0]= 0
ptr[1]= 0
ptr[2]= 0
ptr[3]= 0
ptr[4]= 0

Example 2: Array of Integer


Pointers

Here, we declare an array of integer


pointers and store the addresses of
three integer variables.

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 2/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

#include <stdio.h>

int main(){

int a = 10, b = 20, c = 30;


int *ptr[3] = {&a, &b, &c};

for (int i = 0; i < 3; i++){


printf("ptr[%d]: address: %d value: %d
}

return 0;
}

Output

Run the code and check its output −

ptr[0]: address: 6422040 value: 10


ptr[1]: address: 6422036 value: 20
ptr[2]: address: 6422032 value: 30

Example 3

We can store the address of each


element of a normal array in the
corresponding element of a pointer
array.

#include <stdio.h>

int main(){

int arr[] = {10, 20, 30};


int *ptr[3] = {&arr[0], &arr[1], &arr[2]};

for (int i = 0; i < 3; i++){


printf("ptr[%d]: address: %d value: %d
}

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 3/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

return 0;
}

Output

Run the code and check its output −

ptr[0]: address: 6422032 value: 10


ptr[1]: address: 6422036 value: 20
ptr[2]: address: 6422040 value: 30

Traversing an Array with its


Base Address
When we obtain the base address of
an array (in this case "&arr[0]"), we
can obtain the addresses of its
subsequent elements, knowing that
the pointer increments by the size of
the data type.

Hence, just with the base address


(the name of the array is the same of
the address of the 0th element), we
can traverse an array.

Example 1

Take a look at the following example


#include <stdio.h>

int main(){

int arr[] = {10, 20, 30};


int *ptr=arr;

for (int i = 0; i < 3; i++){


printf("ptr[%d]: address: %d value: %d
}

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 4/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

return 0;
}

Output

Run the code and check its output −

ptr[0]: address: 6422020 value: 10


ptr[1]: address: 6422024 value: 20
ptr[2]: address: 6422028 value: 30

Example 2: Traversing a 2D
Array using a Pointer Array

In this example, we have a 2D array.


The address of the 0th element of
each row is stored in a pointer array.
When traversing, the address stored
in each element of the pointer array,
that points to the 0th element of the
corresponding row, each incremented
to fetch the values in each row.

#include <stdio.h>

int main(){

// 2d array
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
};

int ROWS = 2, COLS = 4;


int i, j;

// pointer
int (*ptr)[4] = arr;

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 5/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

// print the element of the array via poin


for (i = 0; i < ROWS; i++) {
for (j = 0; j < COLS; j++) {
printf("%d ", *(ptr[i]+j));
}
printf("\n");
}

return 0;
}

Output

When you run this code, it will


produce the following output −

1234
5678

Example 3

We don’t really need a pointer array


here, as we can use the name of this
2D array as its base pointer, and
increment it row and column-wise to
fetch the elements in the given 2D
array −

#include <stdio.h>

int main(){

// 2d array
int arr[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
};

int ROWS = 2, COLS = 4;


int i, j;

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 6/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

// pointer
int *ptr = arr;

// print the element of the array via poin


for (i = 0; i < ROWS; i++){
for (j = 0; j < COLS; j++){
printf("%d ", *(ptr + i * COLS + j));
}
printf("\n");
}

return 0;
}

Output

The output resembles that of the


previous code −

1 2 3 4
5 6 7 8

Array of Strings
In C programming, a string is an
array of char data type. Since the
name of an array also represents the
address of its 0th element, a string
can be declared as −

char arr[] = "Hello";

Using the pointer notation, a string is


assigned to a char pointer as −

char *arr = "Hello";

We can then declare an array of char


pointers to store multiple strings as
follows −

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 7/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

char *arr[3] = {"string1", "string2", "string3

Example

The following example has an array


of char pointers that is used to store
the names of computer languages −

#include <stdio.h>

int main(){

char *langs [10] = {


"PYTHON", "JAVASCRIPT", "PHP",
"NODE JS", "HTML", "KOTLIN", "C++
"REACT JS", "RUST", "VBSCRIPT"
};

for(int i = 0; i < 10; i++)


printf("%s\n", langs[i]);

return 0;
}

Output

When you run this code, it will


produce the following output −

PYTHON
JAVASCRIPT
PHP
NODE JS
HTML
KOTLIN
C++
REACT JS
RUST
VBSCRIPT

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 8/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

In this program, "langs" is a pointer


to an array of 10 strings. Therefore,
if "langs[0]" points to the address
5000, then "langs + 1" will point to
the address 5004 which stores the
pointer to the second string.

Hence, we can also use the following


variation of the loop to print the
array of strings −

for (int i = 0; i < 10; i++){


printf("%s\n", *(langs + i));
}

Dynamic Array of Pointers


You can use the malloc() function to
declare and initialize an array of
pointers in a dynamic way.

Example

Take a look at the following example


#include <stdio.h>

int main(){

int *arr = (int *)malloc (sizeof (int) * 5);

for(int i = 0; i < 5; i++){


arr[i] = i;
}
for (int x = 0; x < 5; x++){
printf("%d %d\n", x, arr[x]);
}

return 0;
}

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 9/10
6/16/24, 1:16 PM Initialization of Pointer Arrays in C

Output

When you run this code, it will


produce the following output −

00
11
22
33
44

You can even ask for user input and


assign the values to the elements in
the pointer of arrays −

for(i = 0; i < 5; i++){


scanf("%d", &x);
arr[i] = x;
}

https://www.tutorialspoint.com/cprogramming/c_initialization_of_pointer_arrays.htm 10/10

You might also like