Cloud
Cloud

Reputation: 19331

typedef'd array of pointers - copying values

Good day,

I have a few simple typedefs like so:

typedef int dataType;
typedef dataType* arrayOfNPointers[N];

So, dataType represents int, and the type arrayOfNPointers represents an array of N pointers to int's.

Am I able to reliably perform a deep copy of the contents of one of these arrays to another like so:

#define N (2)
arrayOfNPointers a1 = { 1, 2};
arrayOfNPointers a2;

a2 = a1;    // Safe?

I know that I can directly copy struct's like this, and I know that in general, if I were to try to assign this with int* pointers, it would just qualify as a "shallow copy".

In this case, will I end up with two independent fixed-size arrays that contain the elements {1, 2}?

Thank you!

Upvotes: 2

Views: 134

Answers (3)

Barath Ravikumar
Barath Ravikumar

Reputation: 5836

a2 = a1; // Safe?

No, The statement won't even compile. Your compiler will start screaming.

main.c: error: incompatible types when assigning to type 'arrayOfNPointers' from type 'dataType **'

Upvotes: 2

Yang
Yang

Reputation: 8180

No, you can't assign an array to another. This code will not even compile since you're trying to assign to type arrayOfPointers from type int**.

Consider a simpler example:

int a1[3] = {1, 2, 3};
int a2[3];
a2 = a1;   // will not compile since you're trying to assigning to int[3] from int*

Even if you have type-defined

typedef int IntArray[3];
IntArray a1 = {1, 2, 3};
IntArray a2;
a2 = a1;

This will not compile.

Upvotes: 2

Firstly, watch out:

arrayOfNPointers a1 = {1, 2};

As far as I can tell, this is initialising pointers as if they were integers. This cannot end well. (I'll deal with this more fully later.)

But anyway:

Safe?

Nope. a1 and a2 are both arrays; this means that, among other things, they cannot be assigned to.

   a2 = a1;
// ^^^^ WRONG

Even if this was possible [e.g. if they were pointers], this wouldn't even be a shallow copy - the two [arrays that we are pretending are] pointers would point at the same block of memory. [Only structs can be shallow-copied this way.]

A proper deep copy of an array can be performed in C only with a for-loop:

for (int i = 0; i < N; ++i) {
  a2[i] = a1[i];
}

But you're dealing with pointers, so the above only really counts as a shallow copy. You'll need something more interesting to properly deep-copy this array:

for (int i = 0; i < N; ++i) {
  a2[i] = malloc(sizeof(int));
  *a2[i] = *a1[i];
}

But what's wrong with arrayOfNPointers a1 = {1, 2};?

A lot. {1, 2} is not initialising the integers pointed to; it is initialising the pointers.

Fix 1

Arrays of integers:

typedef DataType arrayOfIntegers[N];
arrayOfIntegers a1 = {1, 2};

Fix 2

Properly initialised arrays of pointers to integers:

DataType arr[N] = {1, 2};
arrayOfNPointers a1 = {&a[0], &a[1]};

Or, if you'd like to use malloced memory, and allocate it later:

arrayOfNPointers a1 = {NULL};

The rules of initializer lists ensure tht all the pointers are initially NULL.

Upvotes: 2

Related Questions