Thread: molloc

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    21

    linked lists

    int mode=0,i=0;
    char filename[15],readletter;
    FILE *fptr;
    struct data{
    char artist[48];
    char album[48];
    char ref[16];
    float cost;
    int year;
    struct data *next;
    };
    struct data *input,*start;
    input->next=NULL;
    start=input;


    switch(mode)
    {
    case 1: /*Reading in data*/
    printf("Enter the artist: ");
    scanf("%s",&input->artist);


    break;

    can some please help with malloc settings

    thanks
    Last edited by ukcpaul; 01-09-2002 at 11:40 AM.

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    21

    ok works now how do i?

    lol my brain has had enough

    ive been going at this c program allday

    int mode=0,i=0;
    char filename[15],readletter;
    FILE *fptr;
    struct data{
    char artist[48];
    char album[48];
    char ref[16];
    float cost;
    int year;
    struct data *next;
    };
    //struct data *input,*start;
    //input->next=NULL;
    //start=input;
    struct data *input = ( struct data* )malloc( sizeof( data ) ),*start = input;

    i changed that and it works thank you


    case 1: /*Reading in data*/
    printf("Enter the artist: ");
    scanf("%s",input->artist); /*use fgets( ( *input ).artist, 48, stdin ); ( *input ).artist[strlen( ( *input ).artist ) - 1] = 0;*/
    printf("/*what do i put here*/");
    break;

    i think its taking data in but what do i put in the printf to check

    thx

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    21

    memory writting error

    VVV thx for your help so far
    but now stuck on entering values

    int mode=0,i=0;
    char filename[15],readletter;
    FILE *fptr;
    struct data{
    char artist[48];
    char album[48];
    char ref[16];
    float cost;
    int year;
    struct data *next;
    };
    struct data *input=(struct data*)malloc(sizeof(data)),*start=input;

    printf("\nEnter the artist: ");
    scanf("%s",input->artist);
    //fgets( ( *input ).artist, 48, stdin ); ( *input ).artist[strlen( ( *input ).artist ) - 1] = 0;
    //printf("%s", (*input).artist);
    //puts((*input).artist );
    printf("Enter the album: ");
    scanf("%s",input->album);
    printf("Enter the reference: ");
    scanf("%s",input->ref);
    //printf("%s", (*input).ref);
    printf("Enter the cost: ");
    scanf("%f",input->cost);
    printf("Enter the release year: ");
    scanf("%d",input->year);

    everything is fine until i get to the float
    then i get a memory error
    i can get round this by making it a char as well but i thought it would auto be a float?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    21

    thx again VVV

    i now feel quite small
    it was so simple i couldnt see it

Popular pages Recent additions subscribe to a feed