Originally Posted by
grumpy
I'm not going to fix your code - you'll learn more by fixing it yourself - but I will point out obvious problems you MUST address if you want your function to work. At present, it would not even compile. But you also have problems that will be acceptable to a compiler, but will not work when the code is executed.
1) Array indices in C start at zero. So, for an array like "char a[9]", valid indices are 0 to 8. a[0] is the first, a[8] is the last. Accessing a[-1] or a[9] gives undefined behaviour.
2) In your loop body, the only array elements accessed are x[1], y[1], and a[1]. You probably want to use x[i], y[i], and a[i], where the value of i needs to run from 0 to 8 (as per point 1).
3) When performing an assignment, the thing being assigned is on the left hand side, not the right. So line 10 should be "a[1] = x[1] + y[1]" rather than "x[1] + y[1] = a[1]" - otherwise the code will not compile at all. Or, combined with my second point, it should probably be "a[i] = x[i] + y[i]"
4) Even though compilers don't necessarily complain when you do it, arrays cannot be returned directly from functions. A pointer is also not an array. Furthermore, your array a is not guaranteed to exist any more after the function returns. Any attempt by the caller of your function to use the array (via the returned pointer) gives undefined behaviour, since the returned pointer points at something that has ceased to exist (i.e. it is a dangling pointer). Common symptoms of undefined behaviour are program crashes. One alternative is to pass the array a as an argument (or, since z is not being used in your function, put results in array z).
5) You haven't shown code which calls the function. However, the caller needs to ensure it passes valid arrays for the arguments x,y,and z. And, as I said in my 4th point, your caller cannot use the returned pointer.
Thank you very much, I was able to fix the major problem, and it pretty much works now except one thing.
This is my full program, it writes random numbers to two different arrays and then adds them both together. The only problem remaining is that the program generates the same pair of random numbers for each array, instead of each array having completely different numbers, they both have the exact same numbers. So when I finish running the program I end up with effectively double of each of the random numbers generated, I would be very grateful if you could point me in the right direction.
Code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <time.h>
#include <winclear.h>
int * filldata(int x[]);
int * add_data(int x[], int y[], int z[]);
int main()
{
srand (time(NULL));
int i;
int dataqq[9];
int atadqq[9];
int final[9];
filldata(dataqq);
filldata(atadqq);
for(i=0; i!= 9; i++)
{
printf("%d\t\t#1\n",dataqq[i]);
printf("%d\t\t#2\n",atadqq[i]);
}
add_data(dataqq, atadqq, final);
for(i=0; i!= 9; i++)
{
printf("%d\t\t#z\n",final[i]);
//printf("%d\t\t#2\n",atadqq[i]);
}
}
int * filldata(int x[])
{
srand (time(NULL));
int i;
for (i=0;i!=9;i++)
{
x[i]=rand()%1000;
}
return x;
}
int * add_data(int x[], int y[], int z[])
{
int i;
for (i=0;i!=9;i++)
{
z[i] = x[i] + y[i];
}
return z;
}