0

..C.. I am trying to figure out how to access my filled array which utilizes a struct with another function which I will use later for sorting, but I can't even print my function that filled the array in my main.

I have loaded the function inside main which works and printing inside the fillArray function works but I will need to access the array from outside the function later on for a bubble sort and a binary search.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SIZE 100

typedef struct Person
{
    char firstname[25];
    char lastname[25];
} person;

person list [SIZE];

person loadPeople(char firstname[25],char lastname[25])
{
    person p;
    strcpy(p.firstname, firstname);
    strcpy(p.lastname,  lastname);
    return p;
}

void fillArray()
{
    list[0] = loadPeople("Bob","Baker");
    list[1] = loadPeople("Bill","Johnson");
    list[2] = loadPeople("John","Finmeister");
    list[3] = loadPeople("Jennifer","Ratblaster");
    list[4] = loadPeople("Shaun","Gares");
    list[5] = loadPeople("Diggy","McDigMaster");
    list[6] = loadPeople("Joanne","TheStore");
}

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

    printf("First homie's name is: %s %s\n",list[0].firstname,list[0].lastname);

    return 0;
}

I just want to print from main recalling from fillArray but right now it only prints:

First homie's name is:

thats it

2
  • 1
    You need to call fillArray() before trying to read data from the array. Commented Oct 29, 2019 at 22:25
  • 1
    Your definition of main() is not valid. Commented Oct 29, 2019 at 22:28

1 Answer 1

2

You need to call fillArray() in order to execute it:

int main(int argc , char *argv[])
{
    fillArray();
    printf("First homie's name is: %s %s\n",list[0].firstname,list[0].lastname);

    return 0;
}

Note that main() should only have two parameters as shown here.

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

Comments

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.