伙计们,我的问题很简单,但我对它背后的理论感到困惑。如果有人能澄清一下,那就太好了。
我想传递某种类型的变量的动态数组,并让一个函数将该类型的变量转换为另一种类型的变量,并在以该新变量的变量数组的形式返回它之前对其进行更多的工作。
这是我不太实用的代码,向您展示我在做什么
#include <stdio.h>
//takes in unsigned char array, and returns unsigned short array
unsigned short foo( unsigned char *array, int size)
{
int i;
unsigned short *LeArray = NULL;
LeArray = (unsigned short*) malloc(size*sizeof(unsigned short));
for ( i = 0; i < size; ++i ) &LeArray[i] = (unsigned char)&array[i];
//do more stuff to that array here before returning it
return *LeArray;
}
int main()
{
int i, ArraySize;
unsigned char *before = NULL;
unsigned short *after = NULL;
ArraySize = 9;
before = (unsigned char*) malloc(ArraySize*sizeof(unsigned char));
for(i = 0; i<ArraySize; i++) before[i] = i+5; //to get some values in
for (i = 0 ; i<ArraySize; i++) printf("\npre:%d\n",before[i]);
after = foo( before, ArraySize );
for (i = 0 ; i<ArraySize; i++) printf("\npost:%d\n",after[i]);
return 0;
}
任何帮助提示或解释都会对我有很大帮助!非常感谢!!