简体   繁体   中英

Working with Arrays and Pointers in ANSI C

I just started learning C and now face some issues. I wanted to create a small address book. The theory is simple: Creating a structure with the address book logic and initializing a field of this structure.

This is my structure:

typedef struct {
    char name[20];
    char first_name[15];
    char number[15];
} t_person;

This is global. In my main function I create a field/array:

t_person persons[MAX_RECORDS]; // MAX_RECORDS = 30

I face the following problem: I want to call a function, which handles the logic for adding a new person. But I'm confused with adresses/pointers, etc.

Calling the Function:

addEntry(&persons); // Passing the Array to the Function

The Function itself:

void addEntry(t_person *persons)
{

    int i = 0; // I know this won't work, because adding
               // a new person will overwrite the previous one
               // Just for testing

    printf("Name: ");
    gets(&persons[i].name);
    printf("First Name: ");
    gets(&persons[i].first_name);
    printf("Number: ");
    gets(&persons[i].number);
}

I understand, that my function awaits a pointer to t_person, but I pass an array of t_person. But I fail to understand, how to modify my function to to accept an array of t_person WITHOUT making it a global. I want to handle this in my main function.

Any tips for me?

You need to add at a particular index so you can pass like this -

addEntry(&persons[i]); // Passing the Array to the Function
                       // start i from 0 to n-1 (for example)

This index i will be changed in calling function itself . So you don't need to track index and worry about it in your function addEntry .

And instead of using gets use fgets -

fgets(persons->name,sizeof persons->name,stdin);

Similarly for all in your function addEntry .

how to modify my function to accept an array of t_person WITHOUT making it a global. I want to handle this in my main function.

You don't need to make it global . Declare it in main and then pass it your function.

You can declare persons as array and you can push on it using addEntry(&persons[i]); with a loop, you program will be like this:

#include <stdio.h>

#define MAX_RECORDS 30

typedef struct {
    char name[20];
    char first_name[15];
    char number[15];
} t_person;

void addEntry(t_person *persons)
{
    //gets is deprecated because it's dangerous, it may cause buffer overflow, it recommanded to use fgets
    printf("Name: ");
    fgets(persons->name, sizeof(persons->name), stdin);
    //gets(persons->name);
    printf("First Name: ");
    fgets(persons->first_name, sizeof(persons->first_name), stdin);
    //gets(persons->first_name);
    printf("Number: ");
    fgets(persons->number, sizeof(persons->number), stdin);
    //gets(persons->number);
}

int main (int argc, char **argv)
{
    t_person persons[MAX_RECORDS]; // MAX_RECORDS = 30
    int i = 0;

    while( i < MAX_RECORDS)
    {
        addEntry(&persons[i]);
        i++;
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM