-1

guys... can u help me apply malloc in my code... here's my code:

#include<stdio.h>
#include<stdlib.h>
struct studentinfo{
       char id[8];
       char name[30];
       char course[5];
}s1;
main(){
    int i;
    FILE *stream = NULL;
    stream = fopen("studentinfo.txt", "a+");

    struct studentinfo s1;
    struct studentinfo array[3];
    for (i =0; i<1; i++){
       printf("Enter Student ID: ");
       scanf("%s", s1.id);
       fflush(stdin);
       printf("Enter Student Name: ");
       gets(s1.name);
       fflush(stdin);
       printf("Enter Student Course: ");
       scanf("%s", s1.course);

       fprintf(stream, "\n%s,\t%s,\t%s", s1.id, s1.name, s1.course);
    }
       fclose(stream);
    getch();
}

i know malloc alots more space than the usual array... but still im having a hard time using it... thanks a lot :)

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
iamanapprentice
  • 411
  • 5
  • 19
  • 37
  • what is the desired behavior you are trying to get? – SingleNegationElimination Dec 03 '10 at 13:27
  • read over ["When asking about code"](http://www.catb.org/~esr/faqs/smart-questions.html#code); ["Writing the Perfect Question"](http://tinyurl.com/so-hints) – outis Dec 03 '10 at 13:41
  • possible duplicate of [Am i using malloc properly?](http://stackoverflow.com/questions/4343753/am-i-using-malloc-properly) – JeremyP Dec 03 '10 at 15:35
  • I think you're at the same institution as @newbie. Newbie, however, made an attempt himself and asked sensible questions. Have a look at https://stackoverflow.com/questions/4343753/am-i-using-malloc-properly/4343797#4343797 – The Archetypal Paul Dec 03 '10 at 13:30
  • Does this answer your question? [Am i using malloc properly?](https://stackoverflow.com/questions/4343753/am-i-using-malloc-properly) – miken32 Oct 21 '22 at 16:59

2 Answers2

0
#include<stdio.h>
#include<stdlib.h>
struct studentinfo{
       char id[8];
       char name[30];
       char course[5];
};
main(){
    int i;
    FILE *stream = NULL;
    stream = fopen("studentinfo.txt", "a+");

    struct studentinfo * s1 = (struct studentinfo *)malloc(sizeof(struct studentinfo));    

    struct studentinfo * array = (struct studentinfo *)malloc(sizeof(struct studentinfo) * 3);
    for (i =0; i<1; i++){
       printf("Enter Student ID: ");
       scanf("%s", s1->id);
       fflush(stdin);
       printf("Enter Student Name: ");
       gets(s1->name);
       fflush(stdin);
       printf("Enter Student Course: ");
       scanf("%s", s1->course);

       fprintf(stream, "\n%s,\t%s,\t%s", s1->id, s1->name, s1->course);
    }
       fclose(stream);
    getch();
}

BTW:
- fflush(stdin) is not portable.
- gets() is dangerous, replace it with fgets()

Huang F. Lei
  • 1,835
  • 15
  • 23
  • Thou shall not cast the malloc return value: http://en.wikipedia.org/wiki/Malloc#Casting_and_type_safety . Also, prefer sizeof(*s1) to sizeof(struct studentinfo), as it prevents you from having to re-edit the sizeof if you change the type of your variable. Pointers are your friends. – haylem Dec 03 '10 at 13:37
0

you don't need to use malloc in your example because you know how many students you'll have at design (I guess, because you for loop ends at a fixed value). When you will know it only at run time, you can:

studentinfo *array;     // declare it as a pointer

// get the number of students (num) in some way

array = (studentinfo *) malloc(num * sizeof(studentinfo));

// use it as a normal array

free(array)   // don't forget to free!

This works because arrays and pointers are considered the same thing.

miken32
  • 42,008
  • 16
  • 111
  • 154
BlackBear
  • 22,411
  • 10
  • 48
  • 86