2

I have an array through whose contents I need to iterate and prep up a new array. I need to do this multiple times, and I don't need to keep the old data once I'm done with it within the inner loop.

Essentially:

double array[dim];
double temparray[dim];

/*initialise array*/

for(...){
    for(...){
        /*replace contents of temparray based on contents from array*/
    }
    array = temparray;
}

However, this code breaks:

error: assignment to expression with array type
array = temparray;
      ^

Any ideas?

9
  • 1
    for (int i=0; i<dim; i++) array[i] = temparray[i]; Commented Mar 14, 2018 at 22:26
  • 5
    You can also memcpy. Commented Mar 14, 2018 at 22:27
  • @squemish Is that really the only way? What about pointer manipulation, to allow me to just replace array's pointer with temparray's? Commented Mar 14, 2018 at 22:27
  • 4
    You have arrays, not pointers, they're not the same thing. Take a look here: c-faq.com/aryptr/index.html Commented Mar 14, 2018 at 22:29
  • 1
    If you don't need to preserve the data, then just use two pointers that you swap between two buffers. No need to do slow hard copies. Any answer suggesting that you do a hard copy has not given this question enough thought. Commented Mar 15, 2018 at 10:38

4 Answers 4

3

You can use a couple of helper pointers and swap their values, instead of copying the arrays every time.

So, if I've understood your intent, you can write something like this:

#include <stdio.h>

void swap_dptr(double **a, double **b)
{
    double *tmp = *a;
    *a = *b;
    *b = tmp;
}

#define dim 5

int main(void) {
    double a[dim];
    double b[dim] = {9, 8, 7, 6, 5};

    double *pa = a;
    double *pb = b;

    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < dim; ++j)
        {
            pa[j] = 1.0 - 0.5 * pb[j];
            printf("%10.6f", pa[j]);
        }
        puts("");
        swap_dptr(&pa, &pb);
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

18 Comments

This looks like something I might be interested in. The swapping of the array pointers in ingenious, as you don't even have to deal with memory leaks.
Any reason why your temporary pointer is of void type though?
@Mox Just to be more general (code reuse). You can use the right type, of course.
@Mox To avoid compiler warnings you may want to cast it swap_ptr((void**)&pa, (void**)&pb);
@sg7 Good point. The OP may choose to just use double pointers, instead.
|
2

Maybe this is what you're after. The code uses the pointers p1 and p2 to point alternatively at array1/array2 or array2/array1 respectively.

#include <stdio.h>

static void dump_array(const char *tag, int num, double *data)
{
    printf("%8s", tag);
    for (int i = 0; i < num; i++)
        printf(" %4.0f", data[i]);
    putchar('\n');
}

int main(void)
{
    int dim = 10;
    double array1[dim];
    double array2[dim];

    for (int i = 0; i < dim; i++)
        array1[i] = i;

    double *p1 = array1;
    double *p2 = array2;

    dump_array("p1:", dim, p1);
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < dim; j++)
            p2[j] = 2 * (p1[j] + 2) + j;
        dump_array("p2:", dim, p2);
        dump_array("array1:", dim, array1);
        dump_array("array2:", dim, array2);
        double *tp = p1;
        p1 = p2;
        p2 = tp;
        putchar('\n');
    }
}

Sample output:

     p1:    0    1    2    3    4    5    6    7    8    9
     p2:    4    7   10   13   16   19   22   25   28   31
 array1:    0    1    2    3    4    5    6    7    8    9
 array2:    4    7   10   13   16   19   22   25   28   31

     p2:   12   19   26   33   40   47   54   61   68   75
 array1:   12   19   26   33   40   47   54   61   68   75
 array2:    4    7   10   13   16   19   22   25   28   31

     p2:   28   43   58   73   88  103  118  133  148  163
 array1:   12   19   26   33   40   47   54   61   68   75
 array2:   28   43   58   73   88  103  118  133  148  163

     p2:   60   91  122  153  184  215  246  277  308  339
 array1:   60   91  122  153  184  215  246  277  308  339
 array2:   28   43   58   73   88  103  118  133  148  163

     p2:  124  187  250  313  376  439  502  565  628  691
 array1:   60   91  122  153  184  215  246  277  308  339
 array2:  124  187  250  313  376  439  502  565  628  691

2 Comments

Would the arrays being 2D arrays complicate this at all? I'm having issues with the changes made in the inner loop persisting. As in, at the end of all loopage, array is still as if it was just initialised.
If the arrays are 2D arrays, you have to be more careful with the pointer types. With double array1[dim1][dim2];, you'd need double (*p1)[dim2];, I think — I've not verified that with a compiler. You should then be able to make appropriate changes to the dump_array code and initialize/edit code, and you should be home and dry.
0

You cannot directly assign the array to another array. For this problem you have 2 option first one is by using pointer and assign the pointer to other pointer. And second is by using loop and assign the value to another array one by one.

#include<stdio.h>
void main(){
int array[100],temparray[100];
int i,n;
scanf("%d", &n); // size of array
for(i=0;i<n;i++)
{
 scanf("%d", &array[i]);
}
for(i=0;i<n;i++){
temparray[i]=array[i];
}
for(i=0;i<n;i++)
{
 printf("%d", temparray[i]);
}
}

The above code is just a small example of achieving the goal.

Comments

-1

The compiler is right, you cannot assign an array directly. You can initiate it directly and then loop through it to change it:

#include <stdio.h>

double array[5] = {1, 2, 3, 4, 5};
double temparray[5] = {0, 0, 0, 0, 0};

void printArrays() {
    for (int i = 0; i < 5; i++) {
        printf("%f\n", array[i]);
    }
    for (int i = 0; i < 5; i++) {
        printf("%f\n", temparray[i]);
    }
}

void arrayCopy() {
    for (int i = 0; i < 5; i++) {
        array[i] = temparray[i];
    }
}

int main() {
    printf("Hello, Arrays!\n");
    printArrays();
    arrayCopy();
    printf("Hello, Arrays!\n");
    printArrays();
    return 0;
}

There are many ways to illustrate it but I've tried to keep it simple above. It just replaces the elements of one array with another.

Comments

Your Answer

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.