S
skumar434
Hi everybody,
I am faceing problem while assigning the memory dynamically to a array
of structures .
Suppose I have a structure
typedef struct hom_id{
int32_t nod_de;
int32_t hom_id;
int32_t hom_type;
int32_t hom_pid;
} hom_data;
I created array of structures in my program so that i can store the
data in array
hom_data arr[];
I want to know how to assign memory to this array of structure at
runtime.
Can anybody point waht is wrong with this code .
I able to compile it but its printing garbage values.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct hom_id{
int32_t nod_de;
int32_t hom_id;
int32_t hom_type;
int32_t hom_pid;
} hom_data;
int main()
{
FILE *fout;
hom_data hom[30];---->how to allocate memory to this array of stuct
dynamically.
int i=0;
/* open input file and test to see if it is empty */
if ((fout = fopen("momtext.txt", "r")) == NULL) {
printf("error ---- file read is empty\n\n");
exit(1); }
/* open output file */
fout = fopen("momtext.txt", "r");
while (fscanf (fout ,"%08x %08x ", &hom.nod_de, &hom.hom_id)
!= 2)
{
fscanf(fout ,"%08x %08x", &hom.hom_type, &hom.hom_pid );
printf("%08x %08x ", hom.nod_de, hom.hom_id);
printf("%s %s %s\n", hom.hom_type, hom.hom_pid);
i++;
}
return 0;
}
Thanks in advance
I am faceing problem while assigning the memory dynamically to a array
of structures .
Suppose I have a structure
typedef struct hom_id{
int32_t nod_de;
int32_t hom_id;
int32_t hom_type;
int32_t hom_pid;
} hom_data;
I created array of structures in my program so that i can store the
data in array
hom_data arr[];
I want to know how to assign memory to this array of structure at
runtime.
Can anybody point waht is wrong with this code .
I able to compile it but its printing garbage values.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
typedef struct hom_id{
int32_t nod_de;
int32_t hom_id;
int32_t hom_type;
int32_t hom_pid;
} hom_data;
int main()
{
FILE *fout;
hom_data hom[30];---->how to allocate memory to this array of stuct
dynamically.
int i=0;
/* open input file and test to see if it is empty */
if ((fout = fopen("momtext.txt", "r")) == NULL) {
printf("error ---- file read is empty\n\n");
exit(1); }
/* open output file */
fout = fopen("momtext.txt", "r");
while (fscanf (fout ,"%08x %08x ", &hom.nod_de, &hom.hom_id)
!= 2)
{
fscanf(fout ,"%08x %08x", &hom.hom_type, &hom.hom_pid );
printf("%08x %08x ", hom.nod_de, hom.hom_id);
printf("%s %s %s\n", hom.hom_type, hom.hom_pid);
i++;
}
return 0;
}
Thanks in advance