3

I am quite new to programming. Memory allocation also, still confuses me. And our professor asked us to make an array of structures wherein users will input the array size. This is to know how many entries will the users enter in the telephone directory.

Here is it so far:

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

struct TelDirectory {
    char name[50];
    char address[100];
    char tel[20];
}; 

int main() {
    int num, add, del, prev = 0;
    char ask;
    *//asking for the number of entries*
    printf("Number of entries: ");
    scanf(" %i", &num);

    struct TelDirectory *entry[num];

    Input(entry, prev, num);
    Display(entry, num);

//Input and Displaying

void Input(struct TelDirectory *entry[], int prev, int num) {
    int i;

    for (i = prev; i < num; i++) {
        entry[i] = (struct TelDirectory *)malloc(sizeof(struct TelDirectory) * num);

        printf("\nEnter name (last, first, middle): ");
        scanf(" %[^\n]", entry[i]->name);

        printf("Enter address: ");
        scanf(" %[^\n]", entry[i]->address);

        printf("Enter telephone number: ");
        scanf(" %[^\n]", entry[i]->tel);    
    }

void Display(struct TelDirectory *entry[], int num) {
    int i, j;
    printf("%i\n", num);
    struct TelDirectory *temp;
    temp = (struct TelDirectory *) malloc(sizeof(struct TelDirectory) * num);
    for (i = 0; i < num; i++) {
        for (j = i+1; j < num; j++) {
            if (strcasecmp(entry[i]->name, entry[j]->name) > 0) {
                temp = entry[i];
                entry[i] = entry[j];
                entry[j] = temp;
            }
        }
    }

    printf("\n\t\t\t\t\tInformation\n");
    printf("------------------------------------------------------------------------------------------------\n");
    printf("Name\t\t\t\t\tAddress\t\t\t\t\tTelephone Number\n");
    printf("------------------------------------------------------------------------------------------------\n");
    for (i = 0; i < num; i++) {
        printf("%-30s\t\t%-30s\t\t%-30s\n", entry[i]->name, entry[i]->address, entry[i]->tel);
    }
    printf("------------------------------------------------------------------------------------------------\n");
}

And, we will also be asking the users if they will update the directory through either INSERTING more entries or DELETING entries. Is it possible to change the structure array size on run time?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
lazyjan
  • 31
  • 1
  • 4
    For that you should use `malloc` to allocate memory dynamically and `realloc` to reallocate it to bigger/smaller size. – Eraklon Feb 15 '20 at 15:45
  • A `capacity` that exponentially increases when the `size` goes over it takes [amortized time complexity](https://stackoverflow.com/questions/1100311/what-is-the-ideal-growth-rate-for-a-dynamically-allocated-array) and may be used when the number of entries is not known beforehand. – Neil Feb 15 '20 at 20:43
  • Oh thank you. But I don't know how will I cast the realloc. So instead of defining the array structure to size num, will I need to make a pointer to the structure array and allocate (malloc) of size num then realloc again if i'll be inserting and deleting? – lazyjan Feb 16 '20 at 04:20

0 Answers0