2

After puting the values in data base in an array and sending the values to addReader within the function itself ,the values are stored successfully , however when going back to main the entered values are gone.

Given the following code : creating the dynamic array with realloc

reader* readerBuilder(reader *rdr, int *readNum){       //building all the readers
    FILE *read;
    int i=1;
    char *str;
    read = fopen("Readers.txt","r");
    checkFile(read);
    str = readFromFile(read);
    while(str != NULL)
    {
        rdr[i-1] = *cutToRdr(str);
        str = readFromFile(read);
        if(str != NULL){
            i++;
            rdr = (reader*)realloc(rdr,sizeof(reader)*i);
            checkreader(rdr);
        }
    }
    fclose(read);
    *readNum = i;
    return rdr;
}

Given the calling function :

reader* addReader(reader *rdr, int *readNum){       //adding a reader from the user
    char string[1000];
    rdr = (reader*)realloc(rdr,sizeof(reader)*(*readNum+1));// build new struct array ( bigger), with the old values
    checkreader(rdr);// check if can get memory
    printf("please enter the I.D. of the student you want to add:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].id=cutToStr(string);// put id in struct
    printf("please add the reader's first name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].first_name=cutToStr(string);// put first name in struct
    printf("please add the reader's last name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].last_name=cutToStr(string);// put last name in struct
    printf("please add the reader's address:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].address=cutToStr(string);// put adress in struct
    printf("please add the reader's phone:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum].phone=cutToStr(string);// put phone in struct
    rdr[*readNum].numToTake = 5;// change value of numbers to tke to 5
    *readNum = *readNum + 1;// rise the number of the readers
    return rdr;// return the new structs array
}

Given the calling for function in main:

rdr=addReader(rdr,readNum);

Defenition of the reader struct :

typedef struct reader{
    int numToTake;
    char *first_name, *last_name, *address, *phone, *id;
}reader;

What am i doing wrong ? Regards,David

EDIT!

Still doesn't work. The new edition of the code :

void addReader(reader **rdr, int *readNum){     //adding a reader from the user
    char string[1000];
    rdr = (reader**)realloc(rdr,sizeof(reader*)*(*readNum+1));// build new struct array ( bigger), with the old values
    checkreader(*rdr);// check if can get memory
    printf("please enter the I.D. of the student you want to add:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->id=cutToStr(string);// put id in struct
    printf("please add the reader's first name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->first_name=cutToStr(string);// put first name in struct
    printf("please add the reader's last name:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->last_name=cutToStr(string);// put last name in struct
    printf("please add the reader's address:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->address=cutToStr(string);// put adress in struct
    printf("please add the reader's phone:\n");
    scanf("%s",string);// get from operant
    rdr[*readNum]->phone=cutToStr(string);// put phone in struct
    rdr[*readNum]->numToTake = 5;// change value of numbers to tke to 5
    *readNum = *readNum + 1;// rise the number of the readers
    //return rdr;// return the new structs array

}

The new calling in main :

addReader(&rdr,readNum);
2
  • What does cutToStr do? If it returns a pointer to the same string, then this string will be overwritten again and again. And is readerBuilder even used? and how is rdr passed to addReader initialized? Commented Jan 12, 2012 at 16:31
  • The "cutstr" get static array of chars and return pointer to dynamic allocated char * ( used for the cells of the struct ). Commented Jan 12, 2012 at 16:43

1 Answer 1

2

You should be passing **rdr:

reader* addReader(reader **rdr, int *readNum)
                         ^^

Also, this adds a new indirection level when you realloc.

With the code you have now you realloc on a copy of the pointer not the actual pointer.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you please explain why ** instead of one * ?
@zahidavid: search pass by value vs pass by reference in c/c++.
I understand now, thank you . Isn't there any other way to do that without using ** (pointer to pointer) ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.