串联使用空指针两个阵列(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.