November 9th, 2013, 04:29 PM

Structures and allocating memory space within a function
Hi. I was just hoping that I might be able to get some pointers from somebody with something I'm trying to do at the moment.
Essentially, what I'm needing to do is:
1) Define a structure that holds the parameters of a quadratic equation, a couple of roots, a complex root, and the discriminant.
2) Write a function which allocates space for a new quadratic and then reads in the three coefficients from the user.
Now I know how to create the structure, I know how to create a function, I know how to make a function read in values from the user, but I haven't got a clue how to make the function allocate space in the memory for that user input, or how to handle the new structure with the function.
So essentially at the moment I'm stuck at the point of creating the structure!
Code:
typedef struct quadratic
{
float square, xcoef, number;
double root1, root2, complexroot, discriminant;
} quadratic;
If somebody could give me a few pointers for how to do the function part that would be much appreciated.
Thanks.
November 9th, 2013, 05:11 PM

Is this in C? Use malloc() and sizeof(quadratic).
If it is in C++, then use the new operator.
Either way, you will have a pointer of type quadratic* that you will use to access the struct and then to finally free()/delete it.
November 14th, 2013, 07:24 AM

Ok, after a week of not being able to put any time towards this due to other work, once again I'm completely stuck.
I think I've got it set up so that I have a function allocating space in the memory for a new quadratic, and that function then being called from within main(), and while when I run it I am getting the function to scan 3 input values and then display them correctly, I still have no idea if the code is actually working as it should, and allocating memory correctly.
Can somebody please tell me where it's wrong, as I'm sure it's wrong in at least one way!
Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct quadratic
{
float square, xcoef, number;
double root1, root2, complexroot, discriminant;
} quadratic;
quadratic *new_quadratic(int len){
quadratic *new_quadratic;
new_quadratic = (quadratic *) malloc (sizeof(quadratic));
scanf("%f%f%f",&(*new_quadratic).square,&(*new_quadratic).xcoef,&(*new_quadratic).number);
printf("%f\n",(*new_quadratic).square);
printf("%f\n",(*new_quadratic).xcoef);
printf("%f\n",(*new_quadratic).number);
free(new_quadratic);
return 0;
}
int main(){
quadratic *test = new_quadratic(3);
}
*EDIT* Ok, having tried to go a bit further I'm even more sure that this isn't even vaguely correct. The next step should be to "Have main() declare a pointer to a quadratic and initialise it by calling the above function. From within main(), print out the three coefficents to check they are correct. " Now as far as I'm aware the code I have above is declaring a pointer to a quadratic and initialising it in a separate function, but I don't know how I'd have it print out the three user inputted values within main() rather than within the function itself as it currently does.
November 14th, 2013, 10:26 AM

No time right now. Will have to look at it later.
I don't like this: scanf("%f%f%f"
I haven't played enough with scanf to know for sure if it is a problem, but I would very much prefer to use: scanf("%f %f %f"
I think the way you had it was telling scanf that the three numbers are jammed up against each other with no spaces between them, in which case how could scanf tell where one ends and the other begins. Again, I'm not sure whether that would happen.
You can verify by printing out the three values. Oops, that's what you are doing. Have you verified that that scanf works as you expect it to?
I'll be back later in the day.
November 14th, 2013, 10:28 AM

Wow, the program has the words but not the guts.
This is like Feynman's story of the Filipinos who after WWII made radio headsets from grass to be like the American airmen.
[code]
Code tags[/code] are essential for python code and Makefiles!
November 20th, 2013, 06:04 AM

My comments:
1) In don't understand what's len parameter for in the function quadratic.
2) You should not free new_quadratic; you need to return its value.
3) I do not recomend using the same name (new_quadratic) for the funtion and the variable.
This is my suggestion:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct quadratic
{
float square, xcoef, number;
double root1, root2, complexroot, discriminant;
} quadratic;
quadratic *new_quadratic(){
quadratic *new_quadratic;
new_quadratic = (quadratic *) malloc (sizeof(quadratic));
scanf("%f %f %f",&(new_quadratic>square),&(new_quadratic>xcoef),&(new_quadratic>number));
printf("%f\n",new_quadratic>square);
printf("%f\n",new_quadratic>xcoef);
printf("%f\n",new_quadratic>number);
/*free(new_quadratic);*/
return new_quadratic;
}
int main(){
quadratic *test = new_quadratic();
free(test);
}