Referencing a variable to an existing array

Viewed 225

I'm pretty new to C and starting to play with pointers. I haven't found a way to assign an array to multiple variables. What I want ideally is:

char myArray[10] = "test";
char (*p)[10] = &myArray;
char anotherArray[10];
anotherArray = *p;

This doesn't work and I don't know why. I have found a way to "copy" the array by using a for loop,

for (int i = 0; i < 10; i++)
{
   anotherArray[i] = myArray[i];
}

I don't know if it's good practice to do it and if there is an easier way.

The array content is not supposed to change so I just want to have a simple way to do this:

firstArr[size] = "content";
secondArr = firstArr;
2 Answers

You can't assign arrays in C, neither by itself nor by dereferencing pointers to arrays, the syntax simply doesn't allow it.

Arrays are normally copied with memcpy. In case they are strings, you can also use strcpy, which copies up until it finds the string null terminator.

In your example, this would be strcpy(anotherArray, *p);. But to use an array pointer of type char (*)[10] is a bit weird practice, it is far more common to use a pointer to the first element of the array. I would recommend that you change your code to this:

#include <stdio.h>
#include <string.h>

int main(void) 
{
  char input[10] = "test";
  char* p = input;
  char anotherArray[10];

  strcpy(anotherArray, p);
  puts(anotherArray);
}

You can't assign an array to multiple variables, but you can assign multiple variables to point to an array.

Pointers are all about memory and the memory that they point to.

Statements such as this assign a fixed amount of memory (10 char sized bytes of memory) to the variable myArray and initialises the contents to contain "test1".

char myArray[10] = "test1";

By definition myArray is actually a pointer to the first memory location, which in this case holds a char of value 't', but it is fixed to that memory.

You can define another pointer to type char and assign it the same value as the pointer to the memory that holds the data "test1" - thus:

char *secondPtr = myArray;

Now secondPtr and myArray both point to the same memory, which contains "test1". There aren't two copies of the data, but it may appear so if you did

printf("myArray %s", myArray);
printf("secondPtr %s", secondPtr);

Now you can use either myArray or secondPtr to alter the same data, which is why pointers should be treated with care.

Now as secondPtr is just a pointer to a char and as such it isn't fixed in the same way that myArray is, so you can do this:

char myArray2[10] = "test2";
secondPtr = myArray;
printf("secondPtr %s", secondPtr);

secondPtr = myArray2;
printf("secondPtr %s", secondPtr);

To copy data from one array to another you can use memcpy, which will copy a specified number of bytes(octets) of memory from one location to another.

The same process is performed by a loop (this is basic code, but not really the best way of performing it as there are no checks on the size of the arrays nor on the number of loop iterations)e.g.

for(int i=0; i<10; i++)
{
  myArray2[i] = myArray[i];
}

this can also be:

secondPtr = myArray2;
for(int i=0; i<10; i++)
{
  myArray2[i] = secondPtr +i;
}
Related