Initialization and assignment of C language pointers

Source: Internet
Author: User
Tags define null integer numbers

1, the initialization of the pointer

When the pointer is initialized, the right operand of "=" must be the address of the in-memory data, cannot be a variable, and cannot be directly used with an integer address value (except Int*p=0; The statement indicates that the pointer is empty). At this point, *p simply means that a pointer variable is defined, and there is no indirect value.

For example:

int a = 25;

int *ptr = &a;

int b[10];

int *point = b;

int *p = &b[0];


if: int *p;

*p = 7;

The compiler (VS2008) will prompt the variable ' p ' is being used without being initialized. The uninitialized variable p is used.

Because P is the address that points to 7, *p = 7 assigns p to the memory, p is not assigned, so p points to the memory location is random, not initialized.

int k;

int *p;

p = &k; Assign a value to P

*p = 7; Assign p to the memory value, i.e. k= 7


2. Assignment of pointers

int *p;

int A;

int b[1];

p = &a;

p = b;

The assignment of the pointer, the left operand of "=" can be *p, or it can be p.

When the left operand of "=" is *p, the data that is stored by the address pointed by P is changed;

When the left operand of "=" is P, the address pointed to by P is changed.

The variable name B of the array represents the first address of the array, so the p=b is correct.


A pointer of the same type is assigned a value:


int val1 = 18,val2 = 19;

int *p1,*p2;

P1 = &val1;

P2 = &val2;

P1 = p2; Notice, P1 points to val2, not to Val1.


Remarks: Initialization and assignment of strings and pointers

Initialization

Char *CP = "ABCDEFG"; This initialization process is to point the pointer CP to the first address of the string, not to pass the value of the string. Because, in the C language, there is no whole mechanism for processing a string

Assignment value:

CP = "ABCDEFG";

*cp= "ABCDEFG";//Error! A string constant is passed its first address, and the value of the string cannot be modified by *CP because the string is a constant, and it simply points the pointer to the string constant


3. Pointer constant

In C, there is no built-in (built-in) method to represent pointer constants, so when we use it, we usually write the form of an integer constant, and then convert it to the appropriate type by forcing the type conversion, such as int *, double *, char *, and so on. So the following approach is not possible: int *p = 0x12345678; The correct way should be: int *p = (int *) 0x12345678; Note that only addresses can be stored in pointers, and it is not possible to assign a non-0-value integer constant expression or other data of a non-address type to a pointer, for this reason. In most computers, memory addresses are indeed represented as unsigned integers, and are represented in 16 notation, but we cannot use integer numbers to represent addresses in the C language, only with pointer constants, because it is used to assign a pointer.

This assignment can also be understood in a different way, in C, when the assignment operator is used, the expression type on the left and right of the assignment operator should be the same, and if not, the assignment operator will attempt to convert the value of the right expression to the left type. So if you write int *p = 0x12345678; The statement compiler will error: ' = ': cannot convert from ' const int ' to ' int * ' because the type of the expression on the left and right of the assignment operator should be the same, and 0x12345678 is an int constant, p is a pointer to type int, The two types are different, so the correct way is: int *p = (int *) 0x12345678;

4. Pointer initialization supplement

ANSI C defines the concept of a 0-pointer constant: an integer-shaped constant expression that has a value of 0, or if such an expression is cast to a void * type, called a null pointer constant, which can be used to initialize or assign to any type of pointer. That is, we can assign 0, 0L, '/0 ', 2–2, 0*5, and (void *) 0 to a pointer of any type, and then this pointer becomes a null pointer, guaranteed by the system that the null pointer does not point to any object or function.

ANSI C also defines a macro null, which is used to represent a null pointer constant. In most implementations of the C language, NULL is defined in the following way: #define NULL ((void *) 0).

There are several common ways to initialize a pointer:

1. Use null or null pointer constants, such as: int *p = NULL, or char *p = 2-2; or float *p = 0;

2. Take an address of an object and assign it to a pointer, such as int i = 3; int *IP = &i;

3. Assign a pointer constant to a pointer, such as: long *p = (long *) 0xfffffff0;

4. Assign the name of an array of type T to a pointer of the same type as: Char ary[100]; char *cp = ary;

5. Assign the address of a pointer to a pointer, such as: int i = 3; int *ip = &i;int **pp = &ip;

6. Assign a string constant to a character pointer, such as: char *CP = "ABCDEFG";

The essence of the initialization or assignment of a pointer is to assign a pointer to it, regardless of how it was obtained, of an address or a similar ( or compatible ) type . Note that for a pointer that is not sure what type to point to, it is best to initialize it to NULL after it is defined, and to validate it when the pointer is dereferenced to prevent the dereference of null pointers . In addition, it is a good practice to provide a valid initial value for any newly created variable in the program, which can help you avoid unnecessary hassles.

5.void * Type pointer

ANSI c defines a void * Type pointer that defines a pointer, but does not specify what type of data it points to. The void * Pointer is a generic pointer that can be converted to and from any other type of pointer (other than a function pointer) without the need for a type cast, but cannot be dereferenced and subscript. The return value of the malloc function in C is a void * pointer, which we can assign directly to a pointer of another type, but it is best to cast the returned pointer to the desired type from a safe programming style point of view and compatibility, in addition, malloc does not satisfy the request by returning a null pointer as a "memory allocation failure" signal, so be aware that the return value pointer is empty.


6. Pointer to pointer

The 5th method of pointer initialization refers to initializing a pointer with the address of a pointer. Recall the previous story: The pointer is a variable, it also has its own address, so it is also an object that can be pointed to by the pointer itself. We can store the pointer's address in another pointer, such as:

int i = 5000;

int *PI = &i;

int **ppi = π

The PPI at this point is a pointer to the pointer that represents the objects:

The address of I is the address of 108,PI, and the address of Pi is the address of the Pi 104,ppi. The PPI dereference will normally get the object that the PPI refers to, and the obtained object is the pointer pi to the INT type variable. To really access I, the PPI must be dereferenced two times, as shown in the following code:

printf ("%d", I);

printf ("%d", *PI);

printf ("%d", **ppi);

The output of the above three statements is 5000.

Initialization and assignment of C language 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.