Problem with pointer and structures in C

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

The forum discussion addresses a coding issue in C related to pointers and structures, specifically within the function total_mark. The original implementation incorrectly attempts to assign an uninitialized variable totalmark to a member of the structure ele709_record. The corrected version initializes totalmark with the value of p->lab_mark.experiment1 and returns it, resolving the issue of returning a random number from memory.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with structures in C
  • Knowledge of pointers and memory management in C
  • Basic debugging skills in C
NEXT STEPS
  • Review C programming best practices for using pointers with structures
  • Learn about memory allocation and initialization in C
  • Explore the use of functions to manipulate structures in C
  • Investigate debugging techniques for C code to identify uninitialized variables
USEFUL FOR

C programmers, software developers, and students learning C who are dealing with pointers and structures, as well as those looking to debug similar issues in their code.

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!