Code
Cấp phát vùng nhớ cho struct trong c
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h> struct student
{ char name; int mssv; int age;
}; struct student* Init()
{ struct student * newStudent = (struct student *)malloc(sizeof(struct student)); if(newStudent == NULL) return NULL; // cáp phát thất bại return newStudent;
} int main()
{ struct student* A = Init(); printf("địa chỉ bắt đầu vùng nhớ: %p\n", A); printf("Kích thước vùng nhớ A đang trỏ tới: %d", sizeof(*A)); return 0;
}
Output:
địa chỉ bắt đầu vùng nhớ: 0x560ed1e8d2a0
Kích thước vùng nhớ A đang trỏ tới: 12