0

In this function, I'm trying to add multiple new records below current records. But after first input I get a crash.

void newRecords(char **firstname, char **lastname, float *score, int num)//integer function to add a record
{
    firstname[num]=(char*)malloc(10*sizeof(char));
    lastname[num]=(char*)malloc(10*sizeof(char));
    int n;

    printf("Enter number of records you want to add: ");
    scanf("%d",&n);
    printf("*USE FORMAT*\nEX)Steve Jobs 99\n");
    printf("-----------------------------------------------------------\n");
    for(int i=0; i<n; i++)
    {
        printf("Enter new student #%d record: ",i+1);
        scanf("%s %s %f",firstname[num+i+1],lastname[num+i+1],&score[num+i+1]);
        num+=1;//number of list is added
    }

    printf("\nNew records:\n");
    printRecords(firstname, lastname, score, num);
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Why are you adding elements to the array before you ask how many records they want to add? – Barmar Apr 14 '20 at 22:02
  • Are you sure the arrays are big enough to hold all the new elements? – Barmar Apr 14 '20 at 22:03
  • You've never allocated any memory for `firstname[num+i+1]` and `lastname[num+i+1]`. Also, you're getting double increments because `num` is being incremented each time through the loop and you're also incrementing `i`. – Barmar Apr 14 '20 at 22:08
  • I added elements to the array to allocate new records. So do I have to use allocation inside for loop and get rid of num increment? This is confusing... I incremented num so that total records size increment. –  Apr 14 '20 at 22:22
  • I posted an answer 10 minutes ago. – Barmar Apr 14 '20 at 22:24
  • I just saw your post. Wow, thank you so much –  Apr 14 '20 at 22:29

2 Answers2

0

You are trying to put the value, for example, first name, into firstname[num+i+1] but your allocation is just for firstname[num]. I think you need to allot memory to 2D array in a function C.

0

You need to allocate memory for the inputs each time through the loop, not just once at the beginning.

Since you're incrementing num in the loop, you don't need to add i to it. And if num is the number of array entries currently in use, then it's also the index of the next element to fill in, so you don't need to add 1 to it. Just use firstname[num] in the scanf() calls.

The function should return num so that the caller will know the new number of array elements that are in use. It would also be best if the caller provided the total size of the arrays, so that this function can avoid a buffer overflow.

int newRecords(char **firstname, char **lastname, float *score, int num)//integer function to add a record
{
    int n;

    printf("Enter number of records you want to add: ");
    scanf("%d",&n);
    printf("*USE FORMAT*\nEX)Steve Jobs 99\n");
    printf("-----------------------------------------------------------\n");
    for(int i=0; i<n; i++, num++)
    {
        printf("Enter new student #%d record: ",i+1);
        firstname[num]=malloc(10*sizeof(char));
        lastname[num]=malloc(10*sizeof(char));
        scanf("%s %s %f",firstname[num],lastname[num],&score[num]);
    }

    printf("\nNew records:\n");
    printRecords(firstname, lastname, score, num);

    return num;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612