Dynamic memory allocation and structure fields

Click For Summary

Discussion Overview

The discussion revolves around the challenges of dynamically allocating memory for fields within a structure in programming. Participants are seeking assistance with specific code errors related to structure definitions and memory management, indicating a focus on technical explanations and potential solutions.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • Some participants express confusion about the use of pointers in their code, suggesting that understanding pointers is crucial for resolving the issues presented.
  • One participant recommends moving the definition of the structure before the main function and emphasizes the need to allocate memory for the structure instance before allocating memory for its fields.
  • Another participant points out that using an uninitialized pointer leads to undefined behavior and suggests declaring an instance of the structure instead of a pointer.
  • There is a proposal to use a typedef for the structure to simplify declarations, allowing for easier usage of the structure in the main function.
  • A participant mentions that they are aware of the operators for accessing structure members but are facing issues in their specific case.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the best approach to take, as there are multiple suggestions regarding whether to use pointers or instances of the structure, and the discussion remains unresolved.

Contextual Notes

Some limitations include potential misunderstandings about pointers, the need for proper memory allocation order, and the implications of using typedefs, which are not fully explored in the discussion.

pairofstrings
Messages
411
Reaction score
7
Please take a look at my program. Please help.

I want to allocate memory dynamically for one field in structure.
I tried to write the code but it has error. I posting a paint document for you to see. Help me please. Please tell me why is my code not working properly and what needs to be done.
Thank you.

dmaandstructures.png
 
Technology news on Phys.org
pairofstrings said:
Please take a look at my program. Please help.

I want to allocate memory dynamically for one field in structure.
I tried to write the code but it has error. I posting a paint document for you to see. Help me please. Please tell me why is my code not working properly and what needs to be done.
Thank you.

dmaandstructures.png
Is this homework? It looks a little too pro.

Your problem is that you don't seem to know what a pointer is or does. Think about it a while. It takes a bit of getting used to. Like s.x is not the same as s->x.
 
The definition for struct student should be moved to before main. The instance of s is OK to have in main. You need to allocate s before attempting to allocate s.name. There are other problems such as sizeof(name), which needs to be sizeof(struct student s.name).
 
pairofstrings said:
Please take a look at my program. Please help.

I want to allocate memory dynamically for one field in structure.
I tried to write the code but it has error. I posting a paint document for you to see. Help me please. Please tell me why is my code not working properly and what needs to be done.
Thank you.

dmaandstructures.png

You want to have
struct student s;
instead of
struct student *s;

In the second case you have an uninitialized pointer, which means it points at garbage. There's no advantage to that.
 
ImaLooser said:
You want to have
struct student s;
instead of
struct student *s;
or allocate s first and then allocate s->name. I'm not sure if you want s to be a pointer to a structure or an instance of a structure. You could also use a typedef to avoid having to use struct with each declaration:

typedef struct
{
int rno;
char *name;
float marks;
}STUDENT;

then later in main you can use

void main()
{
STUDENT s; // declare an instance of s

or if you want a pointer

STUDENT *s = malloc(sizeof(STUDENT));
 
This is how far I got after trying for three days. I know I can access structure members using .* or -> operators but it is not working in this case.
dmaandstructures3.png
 
Last edited:

Similar threads

  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K