Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:14:01+00:00 2026-05-28T04:14:01+00:00

After puting the values in data base in an array and sending the values

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:14:02+00:00Added an answer on May 28, 2026 at 4:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort array/lists/whatever of data based upon the unicode string values
After the suggestion to use a library for my ajax needs I am going
After reading a bit more about how Gnutella and other P2P networks function, I
I putting wmd on my website. After putting code in and giving it a
what does putting () after a word do? sometimes it doesn't work
After reading the Head First Design Patterns book and using a number of other
After reading this question , I was reminded of when I was taught Java
After upgrading to the latest version of TortoiseSVN (1.5.2.13595), it's context menu is no
After being told by at least 10 people on SO that version control was
After lots of attempts and search I have never found a satisfactory way to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.