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 8694873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:53:31+00:00 2026-06-13T00:53:31+00:00

Learning C through Learning C the hard way, and doing some of my own

  • 0

Learning C through “Learning C the hard way”, and doing some of my own exercises. I stumbled upon the following problem.

Let’s say I have the following structure:

struct Person {
  char name[MAX_INPUT];
  int age;
}

In main(), I have declared the following array:

int main(int argc, char *argv[]) { 
  struct Person personList[MAX_SIZE];
  return 0;
}

Now let’s say 2 functions away (main calls function1 which calls function2) I want to save a person inside the array I declared in the main function like so:

int function2(struct Person *list) {
  struct Person *prsn = malloc(sizeof(struct Person));
  assert(prsn != NULL); // Why is this line necessary?

  // User input code goes here ... 

  // Now to save the Person created
  strcpy(prsn->name, nameInput);
  ctzn->age = ageInput;
  list = prsn; // list was passed by reference by function1, does main need to pass the array by
               // reference to function1 before?

  // This is where I get lost:
  // I want to increment the array's index, so next time this function is called and a 
  // new person needs to be saved, it is saved in the correct order in the array (next index)
}

So if I return to my main function and wanted to print the first three persons saved in it like so:

...
int i = 0;
for(i = 0; i < 3; i++) {
  printf("%s is %d old", personList[i].name, personList[i].age);
}
...

Basically how to reference the array across the application while keeping it persistent. Keeping in mind that main does not necessarily call the function directly that makes use of the array. I’m suspecting someone might suggesting declaring it as a global variable, then what would be the alternative? Double pointers? How do double pointers work?

Thank you for your time.

  • 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-06-13T00:53:33+00:00Added an answer on June 13, 2026 at 12:53 am

    Here are a few pointers (no pun intended!) to help you along:

    1. As it stands, the line struct Person personList[MAX_SIZE]; allocates memory for MAX_SIZE number of Person structs. You don’t actually need to allocate more memory using malloc if this is what you are doing.

    2. However, you could save some memory by only allocating memory when you actually need a person. In this case, you want the personList array to contain pointers to Person structs, not the structs themselves (which you create using malloc).

      That is: struct Person * personList[MAX_SIZE];

      When you create the person:

      struct Person * person = (struct Person *) malloc(sizeof(struct Person));

      personList[index] = person;

      And when you use the person list: printf("%s", personList[index]->name);

    3. Arrays don’t magically keep a record of any special index. You have to do this yourself. One way is to always pass the length of the array to each function that needs it.

      void function1(struct Person * personList, int count);

      If you wanted to modify the count variable when you returned back to the calling function, you could pass it by reference:

      void function1(struct Person * personList, int * count);

      A possibly more robust way would be to encapsulate the count and the array together into another structure.

      struct PersonList { struct Person * list[MAX_SIZE]; int count; }

      This way you can write a set of functions that always deal with the list data coherently — whenever you add a new person, you always increment the count, and so on.

      int addNewPerson(struct PersonList * personList, char * name, int age);

    I think that much should be helpful to you. Just leave a comment if you would like something to be explained in more detail.

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

Sidebar

Related Questions

I'm hacking my way through learning Flex and have found some strange behaviour. When
I'm currently learning C through Learning C the Hard Way I am a bit
I'm currently going through the book Learning Python The Hard Way, and I'm trying
I'm working my way through Learning Ruby the Hard Way online; I've just finished
I've been learning my way through ggplot2, and I've made it to using polar
New to wpf and through a learning curve. I have a userControl with a
I'm looking through some code for learning purposes. I'm working through this portion of
I started learning Python through some books and online tutorials. I understand the basic
Very simple question.. I'm into learning Blend (way hard for developer..) So. I'm editing
I'm learning shell scripting, and am finding it hard finding a good 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.