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 represents a pointer variable, and does not mean to take a value indirectly.

Like what:

int a = 25;

int *ptr = &a;

int b[10];

int *point = b;

int *p = &b[0];


hypothesis: int *p;

*p = 7;

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

Since P is the address pointing to 7, *p = 7 assigns the memory to P, 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 a 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 overall mechanism for processing a string

Assignment value:

CP = "ABCDEFG";

*cp= "ABCDEFG";//Error! A string constant passes its first address and cannot be changed by *CP, because the string is a constant, and it is simply a simple pointer to the string constant


3. Pointer constant

There is no built-in (built-in) method in C to represent pointer constants, so when we use it, we usually write the form of an integral constant, and then convert it to the corresponding type by forcing the type conversion, such as: int *, double *, char *, and so on. So what you see later is not working: int *p = 0x12345678; The correct way should be: int *p = (int *) 0x12345678; Note that only the address can be stored in the pointer, you cannot assign a non-0 value integer constant expression or other non-address type data to a pointer, for this reason. In most computers, the memory address is indeed represented by an unsigned integer number, and is represented by a 16 notation, but we cannot use integer numbers in the C language to represent addresses, but only with pointer constants, as 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, assuming it is not, the assignment operator will attempt to convert the value of the right expression to the left type. So suppose to write out int *p = 0x12345678; This 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 the int type, 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 with a value of 0, or an expression that is cast to a void * type, called a null pointer constant, which can be used to initialize or assign pointers of whatever type. That is, we can assign 0, 0L, '/0 ', 2–2, 0*5, and (void *) 0 to a pointer of whatever type, after which the pointer becomes a null pointer, guaranteed by the system that the null pointer does not point to whatever 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 ways that you often use pointers when you initialize them:

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 an address or a pointer of the same type ( or a compatible type ) to it, And no matter how this address is obtained. 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 examine 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 acts as a generic pointer that can be converted to and from other pointers (except function pointers) 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, and we can assign it directly to a pointer of another type, but from a safe programming style perspective and compatibility, you might want to cast the returned pointer to the desired type, plus 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. Review the previous story: The pointer is a variable and it has its own address, so it is itself an object that can be pointed to by pointers. We are able to store the pointer's address in a 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 visit I, you must make two references to the PPI, as seen 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.