OldSchool
OldSchool

Reputation: 2183

How to dynamically allocate array of pointers?

I am currently implementing a program for file copy from one directory to another and in that program i need to allocate memory dynamically for the pointers.So is it possible to allocate memory dynamically for the array of pointers? if yes please guide me.
Thanks...

Upvotes: 0

Views: 58

Answers (1)

ouah
ouah

Reputation: 145829

This dynamically allocates an array of n pointers to char:

char **p;
int n = 42;

p = malloc(n * sizeof *p);

You can then access the array like any array:

int i;

// Initialize all pointers to NULL
for (i = 0; i < n; i++)
{
    p[i] = NULL;
}

Upvotes: 2

Related Questions