I am learning about Dynamic Memory Allocate now and it's hard to understand, especially when the lecturer mentioned about DMA and structure using pointer.
First, at the line 1 in my code, i get confused what "&ptr->eno" means. we initiate ptr as a pointer so it means ptr hold the address of the memory we reserved, lets say ptr hold the address 2046, is that "&ptr->eno" means writing value to the address of 2046 it point to?
Second, at the line 2, how i can print out the value, cause "ptr->eno" contains value "2046" than when i print it out, it gonna give me the number 2046, not the value i try to stored in the memory location 2046.
My code was copied from the lecture note, i tried to run it on Visual studio, it crashed after i input the values. I am so new with C, my assumption may looks stupid and painful to understand. if you can't understand my explanation, please tell me how to use pointer with structure, may be i can figure out my mistake. Thank you
#include<stdio.h>
#include<stdlib.h>
struct emp
{
int eno;
char ename[20];
float esal;
};
void main()
{
struct emp* ptr;
ptr = (struct emp*)malloc(sizeof(struct emp));
if (ptr=NULL)
{
printf("out of memory error");
}
else
{
printf("enter the value of employee: \n");
scanf_s("%d%s%f", &ptr->eno, ptr->ename, &ptr->esal); // line 1
}
printf("the name is: %s \t the number: %d \t the salary: %f", ptr->ename, ptr->eno, ptr->esal); //line2
}