Problem with pointer and structures in C

  • Thread starter Thread starter cs23
  • Start date Start date
  • Tags Tags
    Structures
AI Thread Summary
The discussion revolves around issues with the `total_mark` function in a C program that calculates student marks. The primary concern is that the function is returning an uninitialized variable, leading to unpredictable results. The original code attempts to assign the value of `totalmark` to `p->lab_mark.experiment1`, which is incorrect. Instead, it should assign the value of `p->lab_mark.experiment1` to `totalmark` and then return `totalmark`. A corrected version of the function is provided, which initializes `totalmark` with the value from the structure, ensuring the function returns the expected mark. Additionally, participants emphasize the importance of formatting code for clarity when sharing in forums.
cs23
Messages
64
Reaction score
0
I'm trying to point to the data in struct ele709_record john_doe into the total_mark function. But it's not working, the value returned is not the same as in struct.

#include <stdio.h>

struct lab {

double experiment1;
double experiment2;
double experiment3;
};

struct theory{
double test;
double final;
};

struct ele709_record{
struct lab lab_mark;
struct theory theory_mark;
};

double total_mark(struct ele709_record *p)
{
double totalmark;
p->lab_mark.experiment1 = totalmark;
return totalmark;


}

int main()
{

double john_doe_mark;

struct ele709_record john_doe;


john_doe.lab_mark.experiment1= 90.2;
john_doe.lab_mark.experiment2= 70.5;
john_doe.lab_mark.experiment3= 80.4;
john_doe.theory_mark.test= 82.3;
john_doe.theory_mark.final=79.2;

john_doe_mark = total_mark(&john_doe);

printf("this is his mark %lf\n",john_doe_mark);

}
 
Technology news on Phys.org
In your function total_mark what is double totalmark suppose to be? Your not initializing it, in other words you are returning a random number in memory.
 
Last edited:
camel-man said:
In your function total_mark what is double totalmark suppose to be? Your not initializing it, in other words you are returning a random number in memory.

the function total_mark will be used later to calculate the total mark based on weight percentages of the labs,test and final exam. For now i just want to see if i can point to a member in my structure to the function and then return it
 
So what is it returning and what do you want it to return what exact numbers? Right now you are returning an un assigned variable and you are getting random number in memory.
 
Er, did you really intend to write
Code:
totalmark = p->lab_mark.experiment1;
instead of
Code:
p->lab_mark.experiment1 = totalmark;
?
 
When you post code, please surround it with a [noparse]
Code:
 tag at the top and a
[/noparse] tag at the bottom. This preserves your indentation and makes you code easier to read and understand.
I have done this for your code.
cs23 said:
I'm trying to point to the data in struct ele709_record john_doe into the total_mark function. But it's not working, the value returned is not the same as in struct.
Code:
#include <stdio.h>

struct lab {

	double experiment1;
	double experiment2;
	double experiment3;
};

struct theory{
	double test;
	double final;
};

struct ele709_record{
	struct lab lab_mark;
	struct theory theory_mark;
};

double total_mark(struct ele709_record  *p)
{
	double totalmark;
	p->lab_mark.experiment1 = totalmark;
	return totalmark;


	}

int main()
	{

	double john_doe_mark;

	struct ele709_record john_doe;


	john_doe.lab_mark.experiment1= 90.2;
	john_doe.lab_mark.experiment2= 70.5;
	john_doe.lab_mark.experiment3= 80.4;
	john_doe.theory_mark.test= 82.3;
	john_doe.theory_mark.final=79.2;

	john_doe_mark = total_mark(&john_doe);

	printf("this is his mark %lf\n",john_doe_mark);
	
	}

Change your function as follows:

Code:
double total_mark(struct ele709_record  *p)
{
   double totalmark = p -> lab_mark.experiment1;
   // p->lab_mark.experiment1 = totalmark;
   return totalmark;
}
 
Mark44 said:
When you post code, please surround it with a [noparse]
Code:
 tag at the top and a
[/noparse] tag at the bottom. This preserves your indentation and makes you code easier to read and understand.
I have done this for your code.

Change your function as follows:

Code:
double total_mark(struct ele709_record  *p)
{
   double totalmark = p -> lab_mark.experiment1;
   // p->lab_mark.experiment1 = totalmark;
   return totalmark;
}

Sorry about that.

THANKS SO MUCH!
 
Back
Top