简体   繁体   中英

Dynamic allocation in struct

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_CAPACITY 2

struct student {
    char student[50];
    int age;
};
struct student *arr;
void print(int i){
    for (int j=i; j<=i;j++){
        printf("%s\n%d\n",arr->student,arr->age);
    }
}
void push(int *size, int *capacity){
    printf("push\n");
     int *tmp;
     if(*size > *capacity){
          arr = realloc(arr, 2 * sizeof(arr));;
          *capacity = sizeof(arr) * 2;
     }
}
void add_record(char *name, int age){
    printf("add_record \n");
    static int size = 0;
    static int i=0;
    static int capacity = INITIAL_CAPACITY;
    arr = malloc(INITIAL_CAPACITY * sizeof(arr));
    push(&size,&capacity);
    strcpy(arr[i].student, name);
    arr[i].age = age;
    printf("%d\n", size);
    printf("%d\n", capacity);
    print (i);
    i++;
}
int main()
{   
    add_record("uvhj",34);
    add_record("sakenof",34);
    add_record("sakekbdjb",34);
    add_record("sakenohjm",34);
    return 0;
}

In this code i was trying to dynamically allocate the memory to struct using calloc and malloc. but i keep failing and not able to get the desired output. sorry if this code hurt your eyes i am new to coding

I have create a struct students to handle all your static.

size wasn't updated, capacity should be the number of elements more than the size of the memory.

sizeof was done on a pointer not on the element, so the allocated memory wasn't of the correct size.

Rename push function to check_capacity to match the goal of the function. If there is remaining room ( size < capacity ) do nothing. Else the array should be extended by doubling the size of the array ( capacity *= 2; ) and realloc realloc(arr->data, arr->capacity * sizeof(student));;

Adding an element, is just putting an element at the end. First check if there is remaining room by callning check_capacity , then the position to use to put after the last element is position size as there is element from 0 to size - 1 . Add the element at size position then increase the size : arr->size++;

The outptut is:

add_record 
size: 1
capacity: 2
0: uvhj, 34
add_record 
size: 2
capacity: 2
0: uvhj, 34
1: sakenof, 34
add_record 
size: 3
capacity: 4
0: uvhj, 34
1: sakenof, 34
2: sakekbdjb, 34
add_record 
size: 4
capacity: 4
0: uvhj, 34
1: sakenof, 34
2: sakekbdjb, 34
3: sakenohjm, 34
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INITIAL_CAPACITY 2

typedef struct student {
    char student[50];
    int age;
} student;

typedef struct students {
    int size;
    int capacity;
    student *data;
} students;

void print(students *arr){
    for (int i = 0; i < arr->size; i++){
        student s = arr->data[i];
        printf("%d: %s, %d\n", i, s.student, s.age);
    }
}
void check_capacity(students *arr) {
     if(arr->size < arr->capacity) {
         return;
     }
     arr->capacity *= 2;
     arr->data = realloc(arr->data, arr->capacity * sizeof(student));;
}
void add_record(students *arr, char *name, int age){
    printf("add_record \n");
    check_capacity(arr);
    strcpy(arr->data[arr->size].student, name);
    arr->data[arr->size].age = age;
    arr->size++;
    printf("size: %d\n", arr->size);
    printf("capacity: %d\n", arr->capacity);
    print(arr);
}
int main()
{
    students arr;
    arr.size = 0;
    arr.capacity = INITIAL_CAPACITY;
    arr.data = malloc(INITIAL_CAPACITY * sizeof(student));
    add_record(&arr, "uvhj",34);
    add_record(&arr, "sakenof",34);
    add_record(&arr, "sakekbdjb",34);
    add_record(&arr, "sakenohjm",34);
    free(arr.data);
    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