how to correctly use pointers (pointers as output parameters) and structure body pointers as function parameters
In layman's terms, the pointer acts as an argument to a function, and the external value changes as it changes inside the function.
The following is a small project development essay: (with title related contents in error 3)
Error 1:
built-in.o:in function ' main ':
/root/winshare/imageplayer/main.c:17:undefined reference to ' Fb_open '
But clearly there is a definition. Not the reason, but the file was not added to the project.
Makefile:
#添加顶层目录下的子文件夹 (Note that the directory name is appended with a/)
obj-y + = display/
Then add the corresponding rule under the subfolder
Child Makefile
Obj-y + + FRAMEBUFFER.O
Error 2:
... Error 3:
The compilation passed, but a segment error occurred and the pointer failed. This error further deepens the use of the pointer
The outer int A is used as a function parameter and can be spread out, using the pointer form
The external pointer acts as a function parameter and is able to come out , using pointers as parameter types
The following cases:
Program 1:
void Mymalloc (char *s)//allocating memory in a function, and then returning
{
s= (char *) malloc (MB)
;
void Main ()
{
char *p=null;
Mymalloc (P); The value of p actually or null,p here does not change
if (p) free (p).
Program 2:
A void Mymalloc (char **s)//pointer pointer, dual pointer type
{
*s= (char *) malloc (MB);
void Main ()
{
char *p=null;
Mymalloc (&P); Here the P can get the correct value
if (p) free (p);
}
attached. Important :
In actual development, more like the use of structural body pointers. Because if you don't use a struct pointer, there will be a bunch of dereference in the function.
structure body pointer as function parameter
void Fb_open (struct framebuffer *fb)//Pointer
{...
}
void Main ()
{
struct framebuffer fb0;
Fb_open (&FB0)//Fetch address character &
struct framebuffer* fb1;//need to assign this pointer to the entity
FB1 = (struct framebuffer*) malloc ();
Fb_open (FB1)//At this time does not need to take the address character, fb1 the member's value in Fb_open is changed affects
}
Detailed Example Description
#include <stdio.h> #include <stdlib.h> struct test_t{int *p;
int A;
int b;
int C;
int D;
};
function external use malloc Test function (function parameter is struct type) void func1 (struct test_t * Test) {test->a = 1;
printf ("Test->a =%d.\n", test->a); ////* The following function uses the malloc test function (the function parameter is the struct type)//void Func2 (struct test_t * Test) {test = (struct test_t *) malloc (sizeof (
struct test_t));
Test->b = 1;
printf ("test->b =%d.\n", test->b);
}//...func3. void Func4 (struct test_t **test) {*test = (struct test_t *) malloc (sizeof (struct test_t));
(*test)->d = 1;
printf ("test->d =%d.\n", (*test)->d);
} void Func_p (struct test_t * Test) {test->p = (int *) malloc (sizeof (struct test_t));
int main (void) {//Use Case 1: function outside malloc struct test_t * T1;
T1 = (struct test_t *) malloc (sizeof (struct test_t));
T1->a = 1; Func1 (t1);//This is used, the value of t1->a is changed, and Func4 (struct test_t **test) distinguishes printf ("Test->a =%d.\n", t1->a);
Free (t1);
Sometimes need to malloc in the function, as follows: #if 0//Use case 2: Wrong use, compile pass, run trigger segment error struct test_t * T2;
Func2 (T2);
printf ("test->b =%d.\n", t2->b);
Free (T2); #endif #if 1//But you can declare a pointer member in the structure, and then use//(in fact, the principle and Case 1 is the same, the external need to have an entity, but here also proposed, because you want to say the question of free)//To allocate memory for pointers within the structure body
Often encountered, the structure containing the pointer is also malloc, but sometimes only remember free outermost pointer struct test_t * t0;
T0 = (struct test_t *) malloc (sizeof (struct test_t));
Func_p (t0);
printf ("test->p =%p.\n", t0->p);
Free (T0->P)//Does not trigger a segment error, pay attention to free this pointer, but also pay attention to the first free this pointer, and then free t0 free (t0);
#endif #if 0//Use case 3:struct test_t * T3;
T3 = func3 function that returns a value of struct test_t *;
#endif//Use Case 4: (FUNC4 parameter uses double pointer type) struct test_t * t4;
Func4 (&T4);
printf ("test->d =%d.\n", t1->d);
Free (T4); One of the most commonly used on the above, of course, there are not listed in the use of the way/** Summary * >> function uses pointer type parameters to increase efficiency * (1) External definition pointer variable malloc allocation * (2) function outside of the reference
The two ways in which the needle can get the allocated memory address after malloc the function: double pointer and return value * A lot of programming experience * * return 0;
Content printed: Test->a = 2.
Test->a = 2.
Test->p = 0x81ed020.
Test->d = 1. Test->d = 1.
In addition : In C, there is no difference between "->" and "." When using a struct.
The defined struct body, if it is a pointer, is used when accessing a member->
if the struct variable is defined, it is used when accessing the member.
For example:
struct AAA {
int A;
char b;
};
struct AAA q; Visit the member to use: Q.A;
struct AAA *p; Visit members with:p->a;