1

I was confused on the concept of passing a structure of arrays in C programming. Most examples involve passing an array of structures with the values already provided. I had to ponder, reread, and it clicked when I saw someone take input and put the struct members in each individual element. It got my brain moving and I created the program below. I needed to push the structure array through user-defined functions. I got it to work on CodeBlocks but not in Cygwin. Input is always welcomed.

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

#define NAME 50
#define PHONE 10
#define MAX 5

// Declare a global structure to pass
// it to a function
struct contact{
    char name[NAME];
    char phone[PHONE];
};

//Array of structure
struct contact * friend[MAX]= {0};

//Declaration of the function
void loadMenu(void);
void insertEntry(struct contact *friend);
void displayEntry(struct contact *friend);

int main(){

    loadMenu();
    return 0;
}

void loadMenu(){

    int choice;

    
        printf("\t\tWelcome to Your PhoneBook \n");
        printf("\t\n 1> Insert new Entry");
        printf("\t 2> Display Entry");
        printf("\t 3> Exit\n");
        printf("\n Enter your choice <1-3>: ");
        scanf("%d", &choice);

        switch (choice)
        {
            case 1:
                insertEntry(friend);
                break;
            case 2:
                displayEntry(friend);
                break;
            case 3:
                exit(0);
                break;
            default:
                printf("**Invalid Input.**\n");
        }//end switch

} //end loadMenu

void insertEntry(struct contact *friend){
    char choice1;
    FILE *pWrite;
    int i;

    do{
    if(i == 0){
        //opening the phoneBook file
    pWrite = fopen("phoneBook.txt", "w");
    } else {
        pWrite = fopen("phoneBook.txt", "a+");
    }

        // Make sure do not enter more than MAX number of records
        if(i >= MAX){
        printf("Reached maximum number of entries that can be filled\n");
        break;
        }

        while (1){
            printf("Enter friend's Name\n");
            scanf("%s", friend[i].name);
            if(strlen(friend[i].name) == 0){
                printf("Name cannot be empty\n");
            } else {
                break;
            }
        }

        while (1){
            printf("Enter friend's Phone number\n");
            scanf("%s", friend[i].phone);
            if(strlen(friend[i].phone) == 0){
                printf("Phone number cannot be empty\n");
            } else {
                break;
            }
        }

        fprintf(pWrite, "%s\t%s \n", friend[i].name, friend[i].phone);
        i++;

    fclose(pWrite);

        fflush(stdin);

        printf("Do you to insert another entry? Y or N \n");
        scanf("%c", &choice1);
        } while (choice1 == 'y' || choice1 == 'Y');
    loadMenu();

}// end insertEntry
void displayEntry(struct contact *friend){

   //Print the phone book's valid current entries. Do not display phone book entries that are invalid or NULL (0).
    FILE *pRead;
    int i;
    char reRun;

     do{
    pRead = fopen("phoneBook.txt", "r");
    if (pRead != NULL){
        printf("\nName\t Phone Number\n\n");
        fscanf(pRead, "%s %s", friend[i].name, friend[i].phone);
        while (!feof(pRead)){
        printf("%s\t%s\n", friend[i].name, friend[i].phone);
        fscanf(pRead, "%s%s", friend[i].name, friend[i].phone);
        }

        fclose(pRead);}
    else{
        printf("\n File not opened\n");
    }
      fflush(stdin);

        printf("Press Y to Execute the Program Again \n");
        scanf("%c", &reRun);
        loadMenu();
        } while (reRun == 'y' || reRun == 'Y');

}//end displayEntry
5
  • 1
    Having two versions of the code is confusing. It makes the question unfocused and difficult to understand what you are really asking. Please show just the one version of the code that isn't working. Then give the input, expected result and actual result. Commented Jun 19, 2021 at 22:35
  • I apologize, I will edit my post to have one code. Commented Jun 19, 2021 at 22:38
  • You are creating array each time you go to function. Create it only once in global namespace or in main and then give pointer to this array to each function that will work on this data. Commented Jun 19, 2021 at 22:38
  • I don't see where you're passing a reference to an array or struct anywhere in your code. Nitpick: if your sensibilities are too delicate to write shitter, replacing the i with a * doesn't actually fool anybody. Why not find a different word, like breaks? Commented Jun 19, 2021 at 22:41
  • 1
    @Caleb It's frustration. I've taken meticulous notes- I'm understanding in theory, read stackoverflow, read tutorials, read forums from 2005. I've been sticking to the basics but it isn't working. I use Cygwin and Codeblocks to double check my code. I'm not sure what part of this I'm missing. Commented Jun 19, 2021 at 22:47

1 Answer 1

1

Your problem is that you're not passing anything at all into your various functions. Look at this:

void insertEntry( ) {
    struct contact * con[MAX]= {0};

That means, whenever insertEntry() is called, it'll create a local variable con that is a pointer to an array of contact structures. And when that function exits, poof, con disappears and anything in it goes away. Same thing for your other functions, e.g.:

void displayEntry(){
    struct contact * con[MAX]= {0};

What you want to do is to create that array once, and then pass it into your functions as aa parameter. Those () at the ends of your function names are meant for parameters. So you might have:

void insertEntry(struct contact * con) {
    int i = 0;
    //...

Create the array in your main() function:

struct contact * friends[MAX]= {0};

and then when you call insertEntry include it as a parameter:

insertEntry(friends);

(I've called the array friends just to give it a different name from the parameter.) The idea here is that insertEntry shouldn't create its own array, but rather use the one that you tell it about.

BTW, you can clean up the code some by giving the type struct contact * a name using typedef. I'll leave that alone for now, but you'll surely find examples of how to do that in your textbook.

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

2 Comments

Thank you. Tutorials and my notes make it look easy, I wrote down all the major components of the problem, but executing it isn't the same. My pseudocode is not as specific as it needs to be...I'll reread. It's not clicking, the examples don't show programs with inputs passing an array through structures. It shows an array being passed through a function with assigned values.
I kept reading and looking at various tutorials. I got it to pass through functions and added file handling. It works in Codeblocks but not in Cygwin. I'll take it as a victory.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.