#define NUM_EL 10
int randomArray1[NUM_EL];
int randomArray2[NUM_EL];
int sumArray[NUM_EL];
//Function Protocol
int IntializeArrayWithPointers(int, int, int);
void DisplayArrayDataWithPointers(int);
int AddTwoArraysWithPointers(int,int,int);
int main()
{
*randomArray1=IntializeArrayWithPointers(randomArray1, 0, 1);
*randomArray2=IntializeArrayWithPointers(randomArray2, 10, 11);
*sumArray=AddTwoArraysWithPointers(randomArray1,randomArray2,sumArray);
DisplayArrayDataWithPointers(randomArray1);
DisplayArrayDataWithPointers(randomArray2);
DisplayArrayDataWithPointers(sumArray);
}
int IntializeArrayWithPointers(int pointer1[], int a, int b)
{
int i;
int* pa;
pa= pointer1;
for(i = 0; i < NUM_EL; i++)
{
pa[i] = rand()%(b-a+1)+a;
}
}
void DisplayArrayDataWithPointers(int* p)
{
int i;
for(i = 0; i < NUM_EL; i++)
{
printf("arrayEL[%d] = %d at %p \n",i,p[i],&p[i]);
}
printf("\n");
}
int AddTwoArraysWithPointers(int a[],int b[],int c[])
{
int i;
int *pa;
int *pb;
int *pc;
pa = a;
pb = b;
pc = c;
for(i=0;i<NUM_EL;i++)
{
*(pc +i)=*(pa +i)+*(pb +i);
}
}
第一个元素,在打印数组时,总是显示为 的值NUM_EL
。即使我将rand
s 设置在 0 和 1 之间,也会发生这种情况。其余的都是随机的,就像他们应该的那样。除了每个数组的第一个元素(包括 sum 数组)之外,该代码运行完美。