본문 바로가기
C 언어

C언어 구조체의 동적배열 사용하기

by treeCoder 2021. 2. 8.

C언어 구조체의 동적배열 사용하기를 소개한다.

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

typedef struct {
    int Amount;
    char * name;
} Store;

typedef struct { // array of structs
    Store *array;
    size_t used;
    size_t size;
} Array;

void exit_if_null(void *ptr, char *msg) {
    if (!ptr) {
        printf("unexpected null pointer: %s\n", msg);
        exit(EXIT_FAILURE);
    }
}

void initArray(Array *a, size_t initialSize) { // Allocate initial space

   a->array = (Store *)calloc(initialSize , sizeof(Store));
   exit_if_null(a->array, "init Array");

   a->used = 0;           // no elements used
   a->size = initialSize; // available nr of elements
}

void insertArray(Array *a, Store element) { // Add element to array

    if (a->used == a->size) {
        a->size *= 2;
        a->array = (Store *)realloc(a->array, a->size * sizeof(Store));
        exit_if_null(a->array, "insert Array1");
    }
    
    a->array[a->used].name = (char*)malloc(strlen(element.name) + 1); // Copy name
    exit_if_null(a->array[a->used].name, "insert Array2");

    strcpy(a->array[a->used].name, element.name);

    a->array[a->used].Amount=element.Amount; // Copy Amount

    a->used++;
}

void freeArray(Array *a) { 

    for(int i=0; i<a->used; i++) { // Free all name vars of each arr elems
        free(a->array[i].name);  a->array[i].name=NULL;
    }

    free(a->array); a->array = NULL; // Now free the array 

    a->used = 0;
    a->size = 0;
}

void printArray(Array *a) { 
    for(int i=0; i < a->used ; i++) {
        printf("%03d: %s\n", a->array[i].Amount, a->array[i].name);        
    }
}

int searchArray(Array *a, char *find) { 
    int found = 0;
    for(int i=0; i < a->used && !found ; i++) {
        found = strcmp(  a->array[i].name, find ) == 0 ;   
    }
    return found;
}

void sortArray(Array *a) {  // bubble sort
    int i, j;
    Store tmp;

    for (i = 0; i < a->used - 1 ; i++) {
        int swapped = 0; // false
        for (j = 0; j < a->used - i -1; j++) {
              if (strcmp(a->array[j].name, a->array[j + 1].name ) > 0 ) {
                     tmp = a->array[j];
                     a->array[j] = a->array[j + 1];
                     a->array[j + 1] = tmp;
                     swapped = 1; // true
              }
       }
       if( swapped == 0) break; // if false, then break;
    }
}

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

    Array a;
    Store tmp;

    tmp.name = (char*)calloc(40, sizeof(char));
    exit_if_null(tmp.name, "main");
    initArray(&a, 2);   // Init array

    char test[40];

    for(int i = 0; i < 5; i++ ) {
        tmp.Amount = i*10 + i;

        scanf("%39[^\n]", test); while( getchar() != '\n');

        strcpy(tmp.name, test);
        insertArray(&a, tmp);  // Add elements 
    }

    int fnd = searchArray(&a, "stud2");

    printf("%d\n", fnd);

    sortArray(&a);
    printArray(&a); 
    freeArray(&a); // Free array

    free(tmp.name);

    return 0;
}

 

실행 결과는 다음과 같다.

 

댓글