How to copy array of pointer to another pointer.
My approach this way
int *ptr2[(i-1)*100];
int *ptr1;
ptr1=&ptr2[(i-1)*100];
What is the efficient way to copy so that it takes less cpu cycle.
How to copy array of pointer to another pointer.
My approach this way
int *ptr2[(i-1)*100];
int *ptr1;
ptr1=&ptr2[(i-1)*100];
What is the efficient way to copy so that it takes less cpu cycle.
If you need to duplicate (copy) ptr2
, you need to declare ptr1
with the proper type, allocate room for the ptr1
array, then copy ptr2
's contents over to ptr1
#include <malloc.h>
#include <string.h>
int *ptr2[(i-1)*100];
int **ptr1; // a pointer to a pointer to an int
ptr1 = malloc(sizeof(ptr2));
memcpy(ptr1, ptr2, sizeof(ptr2));
Note: this is an example. Always make sure malloc
has allocated the memory block before using it and free it up when it's not needed anymore (use free
)
On the other hand, if you just want to create an alias to ptr2
int *ptr2[(i-1)*100];
int **ptr1; // a pointer to a pointer to an int
ptr1 = ptr2;
You can use memcpy
to copy values-
int *ptr2[(i-1)*100];
int **ptr1;
prt1=malloc(sizeof(ptr2)); //also remember to free allocated memory.
memcpy(ptr1,ptr2,sizeof(ptr2));