Problem with pointer and structures in C

  • Thread starter cs23
  • Start date
  • Tags
    Structures
In summary, the function total_mark returns a random number in memory. The random number is supposed to be the total mark based on weight percentages of the labs,test and final exam.
  • #1
cs23
66
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
  • #2
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:
  • #3
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
 
  • #4
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.
 
  • #5
Er, did you really intend to write
Code:
totalmark = p->lab_mark.experiment1;
instead of
Code:
p->lab_mark.experiment1 = totalmark;
?
 
  • #6
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;
}
 
  • #7
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!
 

Related to Problem with pointer and structures in C

1. How do I access a specific element in a structure using pointers in C?

To access a specific element in a structure using pointers in C, you can use the arrow operator "->" to access the member of the structure pointed to by the pointer. For example, if you have a pointer variable called "ptr" pointing to a structure with a member called "name", you can access it using "ptr->name".

2. What is the difference between dot operator (.) and arrow operator (->) in C?

The dot operator (.) is used to access members of a structure or union using its actual variable name, while the arrow operator (->) is used to access members of a structure or union using a pointer to that structure or union.

3. How do I allocate memory for a structure using pointers in C?

To allocate memory for a structure using pointers in C, you can use the "malloc()" function. First, declare a pointer variable of the structure type, then use "malloc()" to allocate memory for it. Don't forget to use the "sizeof" operator to specify the size of the structure.

4. How can I pass a structure to a function using pointers in C?

To pass a structure to a function using pointers in C, you can declare a pointer to the structure as a parameter in the function declaration. Then, when calling the function, pass the address of the structure using the "&" operator.

5. How do I free memory allocated to a structure using pointers in C?

To free memory allocated to a structure using pointers in C, you can use the "free()" function. This function takes in a pointer to the memory that needs to be freed and deallocates it. It is important to free memory after using it to avoid memory leaks.

Back
Top