串联使用空指针两个阵列(C) [英] Concatenate two arrays using void pointer (C)

查看:131
本文介绍了串联使用空指针两个阵列(C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要连接的两个相同类型的阵列到用相同类型的单个新的数组。但问题是我必须使用无效指针,不知我的code不会从第三个元素的工作。我在互联网上搜索一下,但好像没有人是有这个问题。

I want to concatenate two arrays of the same type into a single new array with the same type. But the problem is I have to use void pointers, and somehow my code won't work from the third element on. I searched a bit on the internet but seems like noone is having this problem

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

void array_float_fill(float *arr1, float *arr2, int n, int m){
    for(int i = 0;i<n;i++){
        *(arr1+i)=i+n;
    }
    for(int i = 0;i<m;i++){
        *(arr2+i)=i+m;
    }
}

void array_concat(void *arr1, void *arr2, void *arr3, int n, int m,int size){
    for(int i=0;i<n;i++){
        memcpy((arr3+i),(arr1+i),size); 
    }   
    for(int i=0;i<m;i++){
        memcpy((arr3+i+n),(arr2+i),size);
    }
}

int main(int argc, char const *argv[])
{
    int n=10;
    int m=10;
    float f1[n];
    float f2[m];
    array_float_fill(f1,f2,n,m);
    printf("%f",*(f1+3));
    float* f3 = malloc((n+m) * sizeof(float));
    array_concat(f1,f2,f3,n,m,sizeof(float));
    printf("%f",*(f3+3));
    return 0; 
}

我用试了 -loop到每一个元素复制到新阵列,因为该函数将只给我一个指向数组的开始。不知道为什么它不工作。任何帮助和提示会大大AP preciated

I tried it with a for-loop to copy every single element to the new array, because the function will just give me a pointer to the start of the array. Dunno why it doesn't work. Any help and hints would be much appreciated

推荐答案

...实际上不容许标准C. GCC允许它并假定为1的基础操作规模因此它遵循

void* pointer arithmetic ...

... is not actually allowed in standard C. gcc allows it and assumes a base operand size of 1. Thus it follows

您在使用时混淆字节使用索引的memcpy

You are confusing bytes with indexes when using memcpy.

当(非地规范)将整数无效指针,整数的意思(海合会)是一个字节偏移。这不像一个类型的指针,有编译器会做适当的缩放你:

When (non-standardly) adding integer to void pointers, the integer's meaning (in gcc) is that of a byte-offset. This is unlike a typed pointer, there the compiler will do the appropriate scaling for you:

void *p;
int i;
p[i];   // This will be literally p+i after compiling on your compiler

但是,例如:

float *p;
int i;
p[i];   // This will be p+i*sizeof(float) after compiling.

因此​​,而不是...

So instead of ...

for(int i=0;i<n;i++){
    memcpy((arr3+i),(arr1+i),size); 
}   
for(int i=0;i<m;i++){
    memcpy((arr3+i+n),(arr2+i),size);
}

...你有写(这仍然是编译器特定的):

... you got to write (this is still compiler-specific):

for(int i=0;i<n;i++){
    memcpy((arr3+i*size), (arr1+i*size), size);
}
for(int i=0;i<m;i++){
    memcpy((arr3+i*size+n*size), (arr2+i*size), size);
}

......或符合标准的:

... or go standards-conforming:

for(int i=0;i<n;i++){
    memcpy((char*)arr3+i*size, (char*)arr1+i*size, size);
}
for(int i=0;i<m;i++){
    memcpy((char*)arr3+i*size+n*size, (char*)arr2+i*size, size);
}

通过转化成的char * ,你执行1个字节的基本操作数大小(更多precisely,1℃字节),这是你想要提供的是什么您concat函数的一般性质。

By conversion to char*, you enforce a base operand size of 1 byte (more precisely, 1 C byte), which is what you want given the generic nature of your concat function.

请注意,你可以传递任意大踏步的memcpy ,你实际上并不需要的循环;相反,只是做:

Note that you can pass arbitrary strides to memcpy, you do not actually need the loops; instead, just do:

memcpy((char*)arr3, arr1, size*n);
memcpy((char*)arr3+n*size, arr2, size*m);

在这里你做算术时,才需要

转换。

Conversions are only needed where you do arithmetics.

这篇关于串联使用空指针两个阵列(C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆