当前位置:网站首页>C language learning log 1.16

C language learning log 1.16

2022-06-13 04:57:00 Today is also a day without baldness

Pointer arithmetic :

        +n: Add... To a pointer 1 Indicates that you want the pointer to point down n A variable . for example : int a[10]; int *p = a; *(p+1) ---->a[1]

If the pointer is not pointing to the space allocated continuously on one side , Such as arrays , Then this operation is meaningless .

Other operators (+、-、+=、-=) from + You know :“-” Is the pointer pointing forward to the variable .

*p++: Take out p The data referred to comes from , When you're done, drop in p Move to the next position ,* High priority though , But no ++ high , It is often used for continuous space operation of array class , In some CPU On , This can be translated directly into an assembly instruction .

Pointer comparison :

        <,<=,==,>,>=,!= Can do... On the pointer , Compare their addresses in memory , The cell addresses in the array must be linearly increasing .

0 Address :

         In every program 0 Address , however 0 Address is usually an address that can't be touched , So your pointer should not have 0 value , So you can use 0 Address to show special things :

                                                                         The returned pointer is invalid

                                                                        The pointer is not really initialized ( Initialize to 0)

        NULL Is a predefined symbol , stay C Language is used to express 0 Address , Because some compilers don't want you to use 0 To express 0 Address .

The type of pointer :

        No matter what kind of , All pointers are the same size , Because it's all addresses . But pointers to different types cannot be assigned directly to each other , This is to avoid using the wrong pointer .

        Type conversion :void* A pointer indicating that you don't know what to point to , Calculate with char* identical ( But it's not connected ), Pointers can also be converted to types , for example : int *p=&i void*q=(void*)p; It doesn't change p Type of variable referred to , But let the back pass with different eyes p Look at the variables it refers to , from q Just look at it i As int But as void;

What is the pointer used for

1. Use as a parameter when large data needs to be passed in .2. After passing in the array, operate on the array .3. Function returns more than one result , You need to use functions to modify more than one variable .4. Dynamically requested memory .

原网站

版权声明
本文为[Today is also a day without baldness]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280517425899.html