In-depth understanding of one of the C pointers: primary knowledge pointers

Source: Internet
Author: User
Tags variable scope

Simply put, the pointer contains the memory address. The key to understanding pointers is to understand C's memory management patterns. There are three kinds of memory in C:

①, static global memory (life cycle from the beginning of the program to the end of the program, global variables scope is global, static variable scope within the function that defines them);

②, automatic memory (variables declared inside the function, created when the function is called, scope and life cycle are inside the function);

③, dynamic memory (memory allocation on the heap, release as needed, via pointer reference, scope limited to reference pointers);

Let's start with a pointer to life and print its address and value, here p% refers to returning data in 16-binary form:

#include <stdio.h>Main ()
{int num=5; int* pi=&num;printf ("Address of pi is:%p\n Value:%p\n", &Pi , pi);
Address of Pi is:0xbfa98298
value:0xbfa98294}

The indirect reference operator (*) Returns the value pointed to by a pointer variable, commonly referred to as a dereference pointer. We can use the result of the reference operator as an lvalue, "lvalue" means the operand to the left of the assignment operator, and all the left values must be modifiable because they are assigned values.

Ten ;p rintf ("%d\n", num);

There are several different application scenarios for NULL, which may make it misunderstood. Null is assigned to the pointer to indicate that the pointer does not point to anything. Of course he still has his own address, but his value will be empty. A null macro is an integer constant 0 cast to a void pointer. Many header files contain this definition, such as stddef.h,stdblib.h,stdio.h. Gets a null pointer by using a NULL macro. The ASCII character nul is defined as a total of 0 bytes. A NULL statement is a statement that has only one semicolon. A null string is an empty string. Note A pointer that is initialized may contain any value, but a null pointer does not reference any address in memory. When the pointer is null, the IF (pointer) represents false, and vice versa indicates true.

NULL macro:

#define NULL ((void*) 0)
int* pi1 = 0;//1
int* pi2 = NULL//2  

1 and 2 are equivalent. Here, depending on the context, the number 0 is overloaded, representing a null pointer.

A void pointer is a generic type pointer that holds a reference to any data type. Any pointer can be assigned to a void pointer, or it can be converted to the original pointer type, so we should be careful not to arbitrarily convert because different types of pointer lengths are not necessarily the same. Use the sizeof () function to print the length of the pointer:

printf ("size of *pi is:%d\n"sizeof(*PI));

  When the pointer is declared global or static, it is initialized to NULL when the program starts. The actual length of the pointer depends on the machine and compiler used. Different computers give c the data type allocation space using different data models. One operating system may support multiple models, whichever is usually determined by the compiler.

You may encounter predefined types when using pointers:

①, size_t: For the safety of the expression length;

②, ptrdiff_t: For handling pointer arithmetic operations;

③, intptr_t, and uintptr_t: for storing pointer addresses;

The size_t type represents the maximum length that any object in C can achieve. It is an unsigned integer. SIZE_T is the return value type of the sizeof operator and is also a parameter type for many functions such as malloc and strlen. Print size_t can be used with the%zu or%u format specifier.

The intptr_t and uintptr_t types are used to hold pointer addresses. They provide a portable and secure method for declaring pointers, and are the same length as the pointers used in the system.

The pointer has the following types of operators:

* Declaration Pointers

* Solution guide pointer

-Link operator

+ Addition

-Subtraction

= =! = Equal, unequal

> >= < <= greater than or equal to less than or equal

Data pointers can perform the following arithmetic operations:

Add an integer, and the actual number is the product of the byte corresponding to the pointer data type.

subtract integers;

Subtract two pointers, and one pointer minus another pointer to get the difference between the two pointers. The same difference is the number of units, which is the value of the actual address difference divided by the number of bytes of data. The ptrdiff_t type is used to represent a portable way of two pointer differences.

compare pointers; Difference and comparison pointers are often useful in arrays, and can be used to determine the order of elements.

Pointers support multiple layers of indirect references. A pointer pointer is called a "double pointer."

Not to be continued.

In-depth understanding of one of the C pointers: primary knowledge pointers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.