Slicing an array in C

In Python, if I wanted to assign a vector to the x=(1,2)

first two elements y=(0,0,0,0)

, I would do something like y[:1] = x

. Is there an equivalent in C to assign a to the double x[2]={1.,2.}

first two available elements double y[4] = {0.,0.,0.,0.}

? Or do I need loops?

+3


source to share


4 answers


Just try this

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

int main()
{
    double x[2] = {1., 2.};
    double y[4] = {0., 0., 0., 0.};

    memcpy(y, x, 2 * sizeof(*x));
              /* ^ 2 elements of size -> sizeof(double) */

    return 0;
}

      



instead of writing sizeof(double)

which is ok, I did sizeof(*x)

, because if I changed the type x

I wouldn't need to fix memcpy

, but I would also have to change the type y

in this case.

+2


source


You can write a function that is passed in pointer or array, offset and length. The function then allocates space to the new pointer using malloc () and performs a memcpy () or loop-and-copy operation, although memcpy () is probably better. The new pointer is returned to the caller.



+1


source


Yes, this is possible with a function memcpy

. But you have to be careful with data types. You can also copy data from one type to another. Again, as I said, you need to be careful. Otherwise, it might generate a garbage value.

And the second option is a loop as stated in the question.

0


source


Try it. Slightly more low-level than your python method, but welcome to C where sooner or later everything will block the raw memory to be bypassed!

The arraycopy macro applies some size checking. The arraycopyunchecked function is pretty crude.

Functions overlap. Under the hood, they use memcopy(,,)

which can be slower but won't have unexpected results if unexpected anti-aliasing results in overlapping increments.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> //includes mempcy. I know! I know!

#define arraycopy(dest,doffset,source,soffset,count) arraycopy_((dest),(doffset),(source),(soffset),(count),sizeof(*(dest)),sizeof(*(source)))

int  arraycopy_(void*const dest,const size_t doffset,const void*const source, const size_t soffset,const size_t count,const size_t size,const size_t sizecheck){ 
    if(size!=sizecheck){
        return 1;//Incompatible types.
    }
    memcopy(((char*)dest)+doffset*size,((const char*)source)+soffset*size,count*size);
    return 0;
}

int  arraycopyunchecked(void*const dest,const size_t doffset,const void*const source, const size_t soffset,const size_t count,const size_t size){ 
    memcopy(((char*)dest)+doffset*size,((const char*)source)+soffset*size,count*size);
    return 0;
}


int main(void) {

    int errors=0;

    double dest[]={0.0,0.0,0.0,0.0};
    double source[]={1.0,2.0};

    arraycopy(dest,0,source,0,2);

    if(dest[0]!=1.0){
        ++errors;
    }

    if(dest[1]!=2.0){
        ++errors;
    }   

    if(dest[2]!=0.0){
        ++errors;
    }   

    arraycopy(dest,1,source,0,2);

    if(dest[0]!=1.0){
        ++errors;
    }

    if(dest[1]!=1.0){
        ++errors;
    }   

    if(dest[2]!=2.0){
        ++errors;
    }   


    if(errors!=0){
        printf("Errors - %d\n",errors);
    }else{
        printf("SUCCESS");
    }

    double dest1[]={0.0,0.0,0.0,0.0,0.0};
    double source1[]={1.0,2.0,3.0};

    arraycopy(dest1,2,source1,1,2);

    if(dest[2]!=2.0){
        ++errors;
    }

    if(dest[3]!=3.0){
        ++errors;
    }

   return errors==0?EXIT_SUCCESS:EXIT_FAILURE;
}

      

0


source







All Articles