2

I am writing this code using dynamic memory allocation, for student records as indicated, this code suppose to be simple , I am obviously allocating elements in their correct places the right way, but when it comes to printing them, it gives me a "core dumped" error ! what is wrong ?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
    char **firstname;
    char **lastname;
    float *score;
    int number_of_records,i,j,ctr=1,row=15,col=20;
    /*ctr is to keep track of the student's number (makes it easier to
      the user), it starts with (1)*/

    firstname=malloc(row*sizeof(char*));
    for(i=0;i<row;i++)
    {
        firstname[i]=malloc((col+1)*sizeof(char));
    }
     lastname=malloc(row*sizeof(char*));
    for(i=0;i<row;i++)
    {
        lastname[i]=malloc((col+1)*sizeof(char));
    }
    printf("\nPlease indicate number of records you want to enter (min 2, max 15): ");
    scanf("%d",&number_of_records);
    score=malloc(row*sizeof(float));

    printf("\nPlease input records of students\n(enter a new line after"
           "each record), with following format:\nfirst name last name score ");
    for (i=0;i<number_of_records;i++)
    {
        printf("\nEnter record for student %d : ",ctr);
        scanf("%s %s %f",firstname[i],lastname[i],score[i]);

        ctr++; /*ctr is to keep track of student number
                 (makes it easy to the user) */

    }
     for (i=0;i<number_of_records;i++)
    {

        printf("%s %s %f\n",firstname[i],lastname[i],score[i]);
    }
}
aero
  • 372
  • 1
  • 4
  • 18

1 Answers1

4

Please change this:

for (i=0;i<number_of_records;i++)
    {
        printf("\nEnter record for student %d : ",ctr);
        scanf("%s %s %f",&firstname[i],&lastname[i],&score[i]);

        ctr++; /*ctr is to keep track of student number (makes it easy to the       user) */
    }

to this

for (i=0;i<number_of_records;i++)
    {
        printf("\nEnter record for student %d : ",ctr);
        scanf("%s %s %f",firstname[i],lastname[i],&score[i]);

        ctr++; /*ctr is to keep track of student number (makes it easy to the       user) */
    }

And it will work. The reason is that firstname[i] and lastname[i] are already pointers; you do not need to use & operator for them.

P.S. Also, since you are using C, do no cast the returning values from malloc or realloc. It will be done automatically.

codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
  • Removing the & gives me a "core dumped" error at the time of inputting the information. – aero Apr 18 '15 at 20:01
  • For reasons why the return value of `malloc()` shouldn't be casted, see http://stackoverflow.com/a/605858/3488231 – user12205 Apr 18 '15 at 20:02
  • i removed casting before malloc(), It did not help. – aero Apr 18 '15 at 20:15
  • Which compiler are you using? I found this error: `int number_of_records,i,j,ctr=1,row=15,col=20,;` (remove that comma before the semicolon. Afterwards I only changed what I posted above and it worked fine for me.. – codingEnthusiast Apr 18 '15 at 20:19
  • This comma was typing error here on stack-flow, not in the compiler, sorry i'll edit it out, the compiler I am using is Code:: Blocks – aero Apr 18 '15 at 20:21
  • 1
    @aero Can you give us the exact input you tried that triggered the error? So far I am not reproducing it. – user12205 Apr 18 '15 at 20:27
  • Suppose i choose 2 records, the first line :jack moore 98 , the second line : mike blackbird 88 – aero Apr 18 '15 at 20:35
  • 1
    I'm not getting an error. Please edit your question and add your current code, in case you have done any accidental changes. – codingEnthusiast Apr 18 '15 at 20:44
  • @naltipar what compiler are you not getting an error with ? – aero Apr 18 '15 at 21:03
  • @ace , because it did not make a difference , however i just did, same result , in fact when i remove the & i get an error once i enter the info. – aero Apr 18 '15 at 21:14
  • 2
    @aero Read the answer carefully. Remove `&` for `firstname[i]` and `lastname[i]` but not `score[i]`. – user12205 Apr 18 '15 at 21:18
  • @naltipar , sorry i didn't pay close attention to (score) , thanks for help. – aero Apr 18 '15 at 21:30