Thread: I don't understand what i'm doing wrong..

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Question 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);
    }

  2. #2
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    Posts
    69
    didn't went throught the entire code ... but HD should be declared before you can use it

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Exclamation kukskall

    HD and LD are declared, but LD is a null pointer until HD is read. I then allocate memory using malloc. This doesn't work.. Suggestions?

    The only interesting part of the code is this:

    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);
    }

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    hi knutso,

    you need to pass a pointer to pointer to LD struct in order to return that to where are you calling it.
    so instead of:
    > int fetchData(HEAD *HD, LINE *LD){

    use
    Code:
    int fetchData(HEAD *HD, LINE **LD) { 
    	int i = 0; 
    
    	HD->iNoOfLn = 100; // Fetch header 
    	// Allocate memory 
    	if(!(*LD = malloc(HD->iNoOfLn * sizeof(LINE)))) { 
    		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); 
    }
    Code:
    ...
    printf(!fetchData(&HD,&LD) ? "OK\n" : "ERR\n"); 
    ...
    damyan
    Last edited by damyan; 03-15-2002 at 07:36 AM.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329

    Lightbulb kukskall

    Off course! Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM