Dynamic memory allocation and structure fields

AI Thread Summary
The discussion centers around issues with dynamically allocating memory for a field within a structure in a programming context. The original poster is seeking help due to errors in their code related to pointer usage and memory allocation. Key points include the importance of understanding the difference between using a structure instance and a pointer to a structure. It is suggested that the structure definition should be placed before the main function and that memory for the structure should be allocated before attempting to allocate memory for its fields. The discussion also highlights the need to correctly use the sizeof operator and the distinction between accessing structure members with the dot (.) and arrow (->) operators. Additionally, a typedef is recommended to simplify structure declarations. Overall, the conversation emphasizes proper memory management and pointer initialization in C programming.
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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Back
Top