Problem with pointer and structures in C

  • Thread starter Thread starter cs23
  • Start date Start date
  • Tags Tags
    Structures
Click For Summary

Discussion Overview

The discussion revolves around a problem with pointers and structures in C programming, specifically focusing on the implementation of a function intended to calculate a total mark from a struct. Participants are examining issues related to variable initialization and function return values.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant notes that the variable totalmark in the total_mark function is not initialized, suggesting that it returns a random number from memory.
  • Another participant questions what the function is supposed to return and emphasizes that returning an unassigned variable leads to unpredictable results.
  • There is a suggestion that the assignment in the function should be corrected to totalmark = p->lab_mark.experiment1 instead of the current assignment, which is seen as incorrect.
  • One participant provides a revised version of the total_mark function, initializing totalmark with the value of experiment1 from the struct.
  • Multiple participants emphasize the importance of formatting code correctly when posting, suggesting the use of specific tags to maintain readability.

Areas of Agreement / Disagreement

Participants generally agree that the original implementation of the total_mark function has issues related to variable initialization and assignment. However, there is no consensus on the final implementation or the exact intended functionality of the total_mark function.

Contextual Notes

Limitations include the lack of clarity on how the total mark should be calculated based on weight percentages, as mentioned by one participant. The discussion does not resolve the specific requirements for the total_mark function.

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!