-1

I'm trying to create a function for adding 1 element to each of the three pointer arrays (firstArray, lastArray, scoreArray). For this particular assignment, I'm supposed to use pointers and dynamic memory but no structures.

As the program is now, it automatically moves to adding an additional record and waits for all 3 parts of the user input. However, when the program calls the printRecords() function after running the addRecords() function, I get a segmentation fault. I assume this means I'm not passing the changes made in the addRecords() function back to the main therefore, there is nothing stored where the printRecords() function is trying to read. I've tried several times but have gotten nowhere.

How should I alter the 3 problem lines in addRecords() to pass the changes made to the arrays back to the main?

enter image description here

void printRecords(char **firstArray, char **lastArray, float **scoreArray, int n);
void addRecords(char **firstArray, char **lastArray, float **scoreArray, int *j);

int main(int argc, char** argv) {

    int i = 0, n = 0;
    printf("Please indicate the number of student records to be entered:");
    scanf("%d",&n);

    char **firstArray;
    char **lastArray;
    float **scoreArray;

    firstArray = (char **)malloc(n*sizeof(char *));
    lastArray = (char **)malloc(n*sizeof(char *));
    scoreArray = (float **)malloc(n*sizeof(float *));

    for(i=0;i<n;i++)
    {
        printf("Enter Record %d:",i+1);
        firstArray[i] = (char *)malloc(n*sizeof(char));
        lastArray[i] = (char *)malloc(n*sizeof(char));
        scoreArray[i] = (float *)malloc(n*sizeof(float));
        scanf("%s",firstArray[i]);
        scanf("%s",lastArray[i]);
        scanf("%f",scoreArray[i]);
    }

    addRecords(firstArray,lastArray,scoreArray,&n);
    printRecords(firstArray,lastArray,scoreArray,n);

    return (EXIT_SUCCESS);
}

void printRecords(char **firstArray, char **lastArray, float **scoreArray, int n)
{
    int i = 0;
    printf("\nPrinting %d student records....\n",n);
    for(i=0;i<n;i++)
    {
        printf("Record %d: %s - %s - %.2f\n",i+1,firstArray[i],lastArray[i],*scoreArray[i]);
    }

}
void addRecords(char **firstArray, char **lastArray, float **scoreArray, int *j)
{

    int t = *j; //Assigning current number of records to temp variable
    t++;        //Increment by 1
    *j = t;     //Passes the new number of records back to main

    printf("Enter Record %d:",t);

    firstArray[t] = (char *)malloc(t*sizeof(char));
    lastArray[t] = (char *)malloc(t*sizeof(char));
    scoreArray[t] = (float *)malloc(t*sizeof(float));

    scanf("%s",firstArray[t]); **//PROBLEM LINES**

    scanf("%s",lastArray[t]); **//PROBLEM LINES**

    scanf("%f",scoreArray[t]); **//PROBLEM LINES**

    printf("\nNew record added successfully");
    return;

}

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Konin
  • 3
  • 1
  • What is your *input* to the program? Are you *sure* you want to use `n` as the size for the string you allocate? And you don't need `scoreArray` to be an array of arrays. Perhaps you should take a few steps back, [get a couple of good beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start over? – Some programmer dude Nov 05 '17 at 21:02
  • The input should be fairly obvious from the linked image. I can clarify in the post if not. You are correct that I could change the way scoreArray is written but that's not the problem I'm having and as it is should be fine for my purposes. – Konin Nov 05 '17 at 21:11
  • Have you learned about structures yet? Three identically sized arrays sounds like it should be one array of a structure with three elements. – Jonathan Leffler Nov 05 '17 at 21:18
  • The post does state that I'm not to use structures in this particular assignment. – Konin Nov 05 '17 at 21:21
  • 1
    Please don't link images. If you can't embed them inside the question then rewrite it as text inside (text inside the question body is always preferred). – Some programmer dude Nov 05 '17 at 21:51
  • 1
    Also, if you enter `2` for `n`, then think about how long string you could have (remembering that each string is *null terminated*). – Some programmer dude Nov 05 '17 at 21:52

1 Answers1

0

I think your memory allocation for the strings is very confused. You have two layers - the arrays of pointers and the actual strings they point to. You are allocating the arrays once at size n. Oddly, the strings are also allocated at size n?? Those should probably be based on some parameter of the problem. In addRecords, you then allocate the added strings at a variable size based on the current number of records but you don't actually extend the arrays of pointers.

Finally, make sure you enforce any length limitations on the strings when you read in the records so you don't trash memory.

DrC
  • 7,528
  • 1
  • 22
  • 37