0

How to use malloc to allocate memory instead for char name[50]; I don't know these concepts am new for c .

struct student

{
    char name[50];
    int roll;
    float marks;
};

int main()

{
    int c;
    printf("no. of students\n");
    scanf("%d",&c);

    struct student *s;
    s=(struct student *) malloc (sizeof(struct student));
    int i;
    printf("\nstudents information:\n");

    for(i=0;i<c;++i)
    {
        printf("enter the name:");
        scanf("%s",s[i].name);
        printf("enter roll no:");
        scanf("%d",&s[i].roll);
        printf("enter the marks:");
        scanf("%f",&s[i].marks);
        printf("\n");
    }
        printf("\ndetails of all the student:\n");

    for(i=0;i<c;++i)
    {
        printf("the student name:%s\n",s[i].name);
        printf("the student roll no. is:%d\n",s[i].roll);
        printf("the student mark is:%.2f\n",s[i].marks);
        printf("\n");
    }
    return 0;
}
R.A
  • 59
  • 1
  • 1
  • 8
  • 5
    Is this meant to be C ? Or C++ ? It looks like plain C, but you also mention `new`. Please tag appropriately. – Paul R Mar 12 '14 at 07:45
  • 3
    Stackoverflow is not really the place to get introductory information in any subject. If you are new to C and need to get familiar with the basics, then a tutorial or book is the way to go. – dandan78 Mar 12 '14 at 08:04
  • Variables are not declared on advance - I'm not familiar with newer standards, but it's defiantly not ANSI C... – Benesh Mar 12 '14 at 08:06
  • 1
    See also [Do I cast the result of malloc?](http://stackoverflow.com/q/605845/296974) – glglgl Mar 12 '14 at 08:15

3 Answers3

1

With the following statement you have only allocated memory that can occupy only one student.

s = (struct student *) malloc (sizeof(struct student));

But what you need is an array of students sized c, so you have to allocated c times the memory you have allocated now, so you can use them as s[i]:

s = (struct student *) malloc (c * sizeof(struct student));
Arjun Sreedharan
  • 11,003
  • 2
  • 26
  • 34
  • thankyou arjun ur right.. but how to allocate dynamically instead for char name[50]; please help – R.A Mar 12 '14 at 08:16
  • @user3351563 you can't directly allocate memory inside `struct` definition. Have pointers inside the `struct` definition and when you use it, dynamically allocate memory, just like the above. So your `struct student` should have `char* name`. – Arjun Sreedharan Mar 12 '14 at 08:20
  • just copy and edit my program i dont know how to replace ur code instead for char name[50]; .. – R.A Mar 12 '14 at 08:27
  • @user3351563 replace `char name[50]` by `char *name`. Later you can do `s[1].name = (char*) malloc (SOME_SIZE * sizeof(char))` before you put or scanf some value into it. – Arjun Sreedharan Mar 12 '14 at 08:34
0
char name[50];

Declares and allocate an array of 50 chars. If you want to allocate an array dynamically, you can use malloc:

char *name = malloc(n*sizeof(char));

Where n is the desired number of elements (50 in our example).

Benesh
  • 3,398
  • 1
  • 18
  • 38
  • thankyou opd for your fast reply but if i tried for dynamically its shows error please edit my program and paste here .. thankyou – R.A Mar 12 '14 at 08:12
  • @user3351563 sounds like you are compiling it as C++, opd wrote a pure C solution i.e. without casting return value of malloc which is also the correct way to do it. When you use a C++ compiler it whines about return value and you are forced to use a type cast – AndersK Mar 12 '14 at 08:31
0
struct student
{
    char *name;
    int roll;
    float marks;
};


#define NAME_LENGTH 128

int i;
struct student *s = malloc(sizeof(struct student) * c);
for(i = 0; i < c; i++)
    s[i].name = malloc(NAME_LENGTH);

However, there is no reason to do so, as long as NAME_LENGTH is known at compile-time.

Don't forget to free each allocated memory block once they no longer needed.

keltar
  • 17,711
  • 2
  • 37
  • 42