Main content
- Use of structures - definition, assignment, structure pointer
- Use of structures as function parameters
- Use of pointers
Key content focus
1. Use of structures - definition, assignment, structure pointer
2. Use of structures as function parameters - it is best to use structure pointers as parameters instead of structures
Because when the passed structure is used as a parameter, the data volume is large, it will take up a lot of time and space, and the efficiency is very low
Using structure pointers as function parameters is much more efficient than using structure itself as parameters. When you just read without wanting to modify structure value, you can add const to prevent changing structure member values.
(STAFF_T const *p)
3. Use of pointers - Definition, initialization, application of memory space (malloc), and release (free) of pointers
Pointers occupy 4Byte on 32-bit system and 8Byte on 64-bit system
Notice:
Use malloc to apply for space pointers. When not used, you need to release them manually to avoid consuming memory. It is best to reassign the value to NULL, such as p = NULL;
Remember to judge whether it is successful after malloc
Definition and initialization of pointers
int* p;
int i;
p = &i;
Or initialize to NULL
int* p = NULL;
C language code example
This C code successfully runs debugging on VSCode and GDB online
/****************************************************************************** Welcome to GDB Online. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl, C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog. Code, Compile, Run and Debug online from anywhere in world. *******************************************************************************/ #include <> #include <> #include <> typedef struct Staff_t{ char* name; char sex; short age; float salary; char* date; } STAFF_T; void print_msg(STAFF_T const *p); // Example of using structure pointer as function parameters int main() { int size; //STAFF_T sta; // Define a structure //staff = &sta; // Another type of structure initialization STAFF_T* staff = NULL; // Define a structure pointer and initialize it to NULL; // Apply for memory space for the structure, and casting is required (here is the structure pointer type). Note that malloc successfully returns the number of bytes, and returns NULL if it fails. staff = (STAFF_T*)malloc(sizeof(STAFF_T)); // Determine whether the application space is successful. Only when the application is successful can it be used. This step is very important and many people are prone to forget it. if (staff == NULL) { printf("malloc(STAFF_T) ERROR\n"); return -1; } // Here are a few lines of debugging, print out that the memory space occupied by some data types in the 32/64-bit system is different in size size = sizeof(STAFF_T); printf("STAFF_T size = %d\n",size); printf("SIZEOF:\n char*:%lu, char:%lu,short:%lu,float:%lu \n",sizeof(char*),sizeof(char),sizeof(short),sizeof(float)); staff->name = (char*)malloc(20*sizeof(char)); // In C language, both assignment methods below staff->name are OK staff->name = "duruofei";//strcpy(staff->name, "duruofei"); staff->sex = 'M'; staff->age = 29; staff->salary = 15000; staff->date = (char*)malloc(20*sizeof(char)); strcpy(staff->date,"2021-12-09");//strcpy(staff->date, "2021-12-09"); // Pass the structure pointer print_msg(staff); // The parameter is a pointer constant, because here it is just reading the value of staff, and you do not want to modify the content of staff // Finally release the pointer and assign it to NULL. Because the memory address pointed to by the pointer after the pointer is free can be used by other variables, but the pointer itself still points to the original address and can also be used to prevent wild pointers from being free(staff); staff = NULL; printf("Hello World\n"); return 0; } /** * function: print_msg * input: STAFF_T const * * ouput: none * descreption: print massage */ void print_msg(STAFF_T const *p) { // The parameter is a pointer constant, because here it is just reading the value of p, and you do not want to modify the content of p if (p == NULL) { printf("print_msg: p is NULL\n"); } printf("Name:%s, Sex:%c, Age:%d, Salary:%0.2f, Date:%s\n",\ p->name, p->sex, p->age, p->salary, p->date); }
This is the end of this article about the use of C/C++ language structure pointers. For more related C++ structure content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!