0

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...

asked Apr 2, 2014 at 17:14
0

1 Answer 1

2

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;
}
answered Apr 2, 2014 at 17:16
Sign up to request clarification or add additional context in comments.

1 Comment

Or use calloc() to automatically zero the allocated memory

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.