Firstly a quick hello and thanks to the people here for such a helpful resource.
I am very new to C and have run into a problem with program I'm experimenting with. My guess is it is a very simple issue, but I just cannot figure it out.
The gist of the program is to create a very simple stats program which has a struct containing the raw data and variables to hold basic descriptive stats (mean, SD, N). And a series of functions to initialise the data, modify and print.
I'm just learning about pointers and thought I'd try implementing this using pointers as return values.
In the code below, I wanted to test whether my "initialse struct" function was working so added some printf statements into the main function to check. It seems to print the "n" value from the struct, but all other values were completely bizarre. Rather than copy the whole thing, I reduced the programme down to the code below. It still replicates the problem (the mean value is plain wrong).
I have tried various work arounds. Somewhat bizarrely (i.e. I have no idea why it works) if I insert a printf statement into the body of the initialisation function, it seems to print OK.
Here is the out put I got from running that code. Am working using XCode (standard tool)
Any help would be really appreciated....
New dataset, n = 101
New dataset, mean = -12978830180352.000000
The Debugger has exited with status 0.
struct dataSet * pdataSet;
struct dataSet * initDataSet(int n);
int main ()
int n = 5;
struct dataSet * pNewDataSet = initDataSet(n);
printf("\nNew dataset, n = %d", (*pNewDataSet).n);
printf("\nNew dataset, mean = %f", (*pNewDataSet).mean);
struct dataSet * initDataSet(int n)
struct dataSet dataSet1;
dataSet1.pdataSet = &dataSet1; //own address
dataSet1.n = 101;
dataSet1.mean = 8;
return dataSet1.pdataSet; // return a pointer