-1

I am trying to print the records after adding on to the original amount of records, and I am successful in doing so within the addRecords function, but outside of the function I have an error, but I am having understanding how. I'm passing the Names array through pointer and the classSize variable is global. So I don't see why it works in the addRecords function and not in the main funtion. All help is appreciated, thanks in advance.

#include <stdio.h>
#include <stdlib.h>

#define STRINGSIZE 21
int classSize;
void addRecords( char **Names);

int main() {
    char **Names;
    int i;

    //User will be able to choose how many records he woudld like to input.
    printf("Please indicate number of records you want to enter:\n");
    scanf("%d", &classSize);

    Names = malloc(classSize*sizeof(char*));

    for (i = 0; i < classSize; i++) {
        Names[i] = malloc(STRINGSIZE * sizeof(char));
    }

    printf("Please input records of students (enter a new line after each record), with following format: first name....\n");

    for (i = 0; i < classSize; i++) {
        scanf("%s", *(Names + i));
    }

    for (i = 0; i < classSize; i++) {
        printf("%s ", *(Names+i));
        printf("\n\n");
    }

    addRecords(Names);

    printf("SECOND PRINT\n\n");

    for (i = 0; i < classSize; i++) {
        printf("%s \n", *(Names+i));
    }
}

void addRecords(char **Names) {
    int addition, i, temp;

    temp = classSize;
    printf("How many records would you like to add?\n");
    scanf("%d", &addition);
    classSize += addition;

    Names = realloc(Names, (classSize) * sizeof(char*));

    for (i = temp; i < (classSize); i++) {
        Names[i] = malloc(STRINGSIZE * sizeof(char));
    }

    printf("Please input records of students (enter a new line after each record), with followingformat: first name....\n");

    for (i=temp; i<classSize; i++) {
        scanf("%s", *(Names +i));
    }
    printf("\n\n");
    printf("FIRST PRINT\n\n");

    for (i = 0; i < classSize; i++) {
        printf("%s \n", *(Names+i));
    }

    printf("\n\n");
}
antonpp
  • 2,333
  • 23
  • 28
Kenshin
  • 177
  • 1
  • 9
  • http://stackoverflow.com/a/27200719/4082723 – 2501 Nov 30 '14 at 00:36
  • @2501 I messed around with making Names a gobal variable and I honestly don't know how to go about passing it by reference. Any tips? – Kenshin Nov 30 '14 at 00:40
  • 1
    what error are you getting? – ryanpattison Nov 30 '14 at 00:46
  • The second print call is looking at a vacant memory space, and I am trying to figure out how to make the realloc in the addRecords function global. @rpattiso – Kenshin Nov 30 '14 at 00:48
  • 1
    @kenshin Jean-Baptiste Yunès answer to your [other question](http://stackoverflow.com/questions/27200661/reallocating-memory-and-adding-a-string-at-the-reallocated-memory-space-in-c/27200719#27200719) answers this problem. You need to add `return Names` to `addRecords` and update its return type, and make the call as `Names = addRecords(Names)` in main. – ryanpattison Nov 30 '14 at 00:55
  • use `%20s` as the scanf specifier to avoid problems associated with buffer overflows – M.M Nov 30 '14 at 00:56

1 Answers1

1

C uses pass-by-value. The Names inside addRecords is a different variable to Names inside main. A copy is created when you call the function.

To fix this you either need to pass Names by reference, or make it a global variable, or return the updated copy via function return value.

In your actual code, attempting to use Names in main() after it had been passed to realloc in the function, causes undefined behaviour (realloc makes its argument indeterminate).

M.M
  • 138,810
  • 21
  • 208
  • 365