I don't understand what i'm doing wrong..
I'm unable to allocate memory for my array of structs.. Any clues to what i'm doing wrong? The
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <stdlib.h>
#include <conio.h>
typedef struct _head_data{
int iNoOfLn;
}HEAD;
typedef struct _line_data{
int iLnNo;
char achName[25];
}LINE;
int fetchData(HEAD *HD, LINE *LD){
int i = 0;
HD->iNoOfLn = 100; // Fetch header
// Allocate memory
if(!(LD = malloc(HD->iNoOfLn * sizeof(*LD)))){
printf("Error allocating memory\n");
exit(0);
}
// Read lines
for(i=0;i<HD->iNoOfLn;i++){
LD[i].iLnNo = i+1;
sprintf(LD[i].achName,"Item no: %03d", i+1);
}
return(0);
}
int main(){
HEAD HD;
LINE *LD = 0;
int i;
printf(!fetchData(&HD,LD) ? "OK\n" : "ERR\n");
for(i=0;i<HD.iNoOfLn;i++)
printf("No: %d, Name: %s\n",LD[i].iLnNo, LD[i].achName);
getch();
return(0);
}