C Language Pointers

Source: Internet
Author: User

pointers, references, and values

What is a pointer? What is a memory address? What is the value of a pointer? A pointer is a variable that stores the memory address of a computer. In this tutorial "References" represents the computer memory address. Reading data from the memory pointed to by the pointer is called the value of the pointer. Pointers can point to variable addresses of specific types, such as int, long, and double. The pointer can also be a void type, a null pointer, and an uninitialized pointer. All of these pointer types are discussed in this article.

Depending on where you appear, the operator * can be used to declare either a pointer variable or a pointer value. When used to declare a variable, * means that a pointer is declared here. Other cases use * to indicate the value of the pointer.

& is an address operator used to refer to a memory address. By using the & operator before the variable name, we can get the memory address of the variable.

// declares an int pointer int *ptr; // declares an int value int 1 ; // assigns a reference to the pointer to an int value ptr = &val; // value the pointer to print the contents stored in the pointer address int deref = *ptr;printf ("%d\n", Deref);

Line 2nd, we declare an int pointer through the * operator. Then we declare an int variable and assign a value of 1. We then initialize our int pointer with the address of the INT variable. Next, the int pointer is evaluated, and the int pointer is initialized with the memory address of the variable. Finally, we print out the output variable value, the content is 1.

The &val of line 6th is a reference. After the Val variable declares and initializes the memory, by using the address operator before the variable name & we can directly reference the memory address of the variable.

Line 8th, we once again use the * operator to value the pointer, you can directly get the data in the memory address pointed to by the pointer. Since the type of the pointer declaration is int, the value taken is the int value stored by the memory address pointed to by the pointer.

Here you can compare pointers, references, and values into envelopes, email addresses, and houses. A pointer is like an envelope, and we can fill it with a mailing address. A reference (address) is like an e-mail address, which is the actual address. The value is like the house of the address. We can erase the address on the envelope and write another address we want, but this behavior has no effect on the house.

void pointer, null pointer, and uninitialized pointer
A pointer can be declared as a void type, such as void *x. A pointer can be assigned a value of NULL. After a pointer variable is declared but not assigned, it is called an uninitialized pointer.

int*uninit;//int pointer not initializedint*nullptr = NULL;//Initialize to nullvoid*vptr;//void pointer not initializedintval =1;int*iptr;int*castptr;//void types can store pointers or references of any typeIptr = &val;vptr=iptr;printf ("iptr=%p, vptr=%p\n", Iptr, vptr); //by showing the transformation, we can turn a void pointer into//int pointer and take valueCastptr = (int*) vptr;printf ("*castptr=%d\n", *castptr); //print null and uninitialized pointersprintf"uninit=%p, nullptr=%p\n", UnInit, nullptr);//do not know what you will get the return value, will be a random spam address? //printf ("*nullptr=%d\n", nullptr);//a segment error is generated here//printf ("*nullptr=%d\n", nullptr);

Execute the above code and you will get output similar to the following for different memory addresses.

iptr=0x7fff94b89c6c, vptr=0x7fff94b89c6c*castptr=1uninit=0X7FFF94B89D50 , nullptr= (nil)

Line 1th We declare an uninitialized int pointer. All pointers are uninitialized before the assignment is null, a reference (address), or another pointer. Line 2nd We declare a null pointer. The 3rd line declares a void pointer. Lines 4th through 6th declare an int value and several int pointers.

Line 9th to 11, we assign a reference to the int pointer and assign the int pointer to a void pointer. The void pointer can hold various other pointer types. Most of the time they are used to store data structures. Notice that on line 11th we print the addresses of the int and void pointers. They now point to the same memory address. All pointers store memory addresses. Their type only works when values are taken.

In line 15th to 16th, we convert the void pointer to the int pointer castptr. Note that the conversion needs to be shown here. Although the C language does not require a display conversion, this increases the readability of the code. We then take the value of the Castptr pointer, which is a value of 1.

Line 19th is interesting to print uninitialized pointers and null pointers here. It is important to note that the uninitialized pointer has a memory address and is a garbage address. Do not know what the value of this memory address points to. This is why you should not take a value on an uninitialized pointer. The best thing is you get a junk address. Next you need to debug the program, and the worst case scenario will cause the program to crash.

The null pointer is initialized to O. Null is a special address, with a null-assigned pointer pointing to an address of 0 instead of a random address. Valid only if you are ready to use this address. Do not take a value on a null address, or a segment error will occur.

Pointers and Arrays

The C-language array represents a contiguous memory space used to store multiple objects of a specific type. In contrast, pointers are used to store a single memory address. Arrays and pointers are not of the same structure and therefore cannot be converted to each other. The array variable points to the memory address of the first element of the array.

An array variable is a constant. You cannot assign a pointer to an array variable, even if the pointer variable points to the same address or to a different array. It is also not possible to assign an array variable to another array. However, it can be confusing to assign an array variable to the pointer. When assigning an array variable to a pointer, it is actually assigning the address to the first element of the array to the pointer.

int myarray[4] = {1,2,3,0}; int *ptr = myarray;printf ("*ptr=%d\n", *// An array variable is a constant and cannot be assigned the following value // myarray = ptr // myarray = Myarray2 // myarray = &myarray2[0]

Line 1th Initializes an int array, and line 2nd initializes an int pointer with an array variable. Since the array variable is actually the address of the first element, we can assign the address to a pointer. This assignment is the same as the int *ptr = &myarray[0] effect, and the first element address of the array is shown to be assigned to the PTR reference. It is important to note here that the pointer needs to be consistent with the element type of the array, unless the pointer type is void.

Pointers and structures

Just like an array, a pointer to a struct stores the memory address of the first element of the struct body. Like an array pointer, a struct's pointer must be declared to be consistent with the struct type, or declared as a void type.

structPerson {intAge ; Char*name;};structPerson first ;structPerson *ptr; First.age= +;Char*fullname ="Full name"; First.name=fullname;ptr= &First ; printf ("age=%d, name=%s\n", First.age, Ptr->name);

Line 1th to 6th declares a person struct, a variable that points to a person struct and a pointer to the person structure body. The 8th Behavior age member assigns an int value. Line 9th to 10th we declare a char pointer and assign a value to a char array and assign it to the struct name member. Line 11th We assign a person struct reference to a struct variable.

Line 13th We print the age and name of the struct instance. It is important to note that there are two different symbols, '. ' and '. ' struct instances can access the age variable by using the '. ' Symbol. For pointers to struct instances, we can access the name variable through the '-a ' symbol. You can also access the name variable by using (*ptr). Name.

Summarize

Hopefully, this brief overview will help you understand the different pointer types. In subsequent posts we will explore other types of pointers and advanced usage, such as function pointers.

Please ask questions and give comments.

Reprinted from: http://blog.jobbole.com/25409/

C Language Pointers

Related Article

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.