-3

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.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
sam
  • 85
  • 2
  • 7
  • 4
    You are not "copying" an array. You only asign the address of the array to a pointer variable. That costs no CPU time (hardly). Please explain better waht you want to do. – Paul Ogilvie Aug 04 '15 at 07:51
  • So you're having an `ptr2` array of size `(i - 1) * 100`, and then you index it with `(i - 1) * 100`. That index is 1 too far. – meaning-matters Aug 04 '15 at 07:55
  • 1
    "memcpy()" is the choice of weapons, when it comes to copying arrays. – Peter Miehle Aug 04 '15 at 08:21
  • I don't see why the question is being downvoted. People are always struggling with pointers and we are here to help/explain. – Pynchia Aug 04 '15 at 08:22
  • @Pynchia there are already a gread deal of resources of what pointers and arrays are or aren't. It's not that complicated a subject, either. So people are fed up of nonsense questions about arrays and pointers that show absolutely no research effort. – Quentin Aug 04 '15 at 08:46
  • possible duplicate of [faster alternative to memcpy?](http://stackoverflow.com/questions/2963898/faster-alternative-to-memcpy) – Evil Dog Pie Aug 04 '15 at 09:28

2 Answers2

3

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;
Quentin
  • 62,093
  • 7
  • 131
  • 191
Pynchia
  • 10,996
  • 5
  • 34
  • 43
  • What do you think `sizeof(ptr2)` will return? – qrdl Aug 04 '15 at 08:45
  • I see your point. siezof(ptr2) return the whole array occupation. (i.e. intsize*(i-1)*100). Please feel free to describe the accurate wording then. Answer edited. Downvoting before discussing is impolite. – Pynchia Aug 04 '15 at 08:47
  • @qrdl `ptr2` is an array containing `(i-1)*100` elements of type `int*`, so `sizeof(ptr2)` will return the number of bytes occupied by that array, which will depend on the hardware architecture. The name is confusing, as is placing the `*` next to the variable name rather than next to the `int`. A clearer declaration might be : `int* arrayOfPointersToIntegers[(i-1)*100];`. Overall the code in the answer will behave as intended. – Evil Dog Pie Aug 04 '15 at 09:14
  • @Pynchia This answer doesn't really address the question regarding the efficiency of using `memcpy` as opposed to some other, hand-spun technique. I suspect that most implementations of `memcpy` (being a library function) are pre-optimised for performance, although in embedded software this isn't always true for some specific tasks. – Evil Dog Pie Aug 04 '15 at 09:19
0

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));
ameyCU
  • 16,489
  • 2
  • 26
  • 41