Simple understanding of C language pointers and C language pointers

Source: Internet
Author: User

Simple understanding of C language pointers and C language pointers

Put a program first

What is a pointer? There are several main online sayings:

1. the pointer is a variable that stores the address.

2. the pointer is an unsigned integer.

3. the pointer is an integer variable.

4. the pointer is the address.

5. pointer is a type

 

1. First, the pointer is definitely notInteger variableSizeof indicates four bytes and can be output in % d.

It is not so much an integer as an unsigned integer, but a pointer isUnsigned integerThere are also obvious problems. Here we use the program to overwrite these two statements first.

#include"stdlib.h"#include "stdafx.h"int main(){    int *p1 = NULL;    int a = 20;    unsigned int b = 30;    p1 = a;//error    p1 = b;//error    p1 *= 5;//error    return 0;}

Copy the unsigned and integer variables to the pointer variables respectively. If the compiler prompts an error, it means that the pointer is not an integer or an unsigned integer. (Easy to understand ).

And integer variables can be used as multiplication pointer variables.

2.A pointer is a variable that stores the address.

Or the pointer is the address. Use the following program to test it.

Int a = 10; int B; B = & a; // error: cannot be converted from int * to int

This proves that common variables cannot store addresses, but pointer variables can store addresses (but this only indicates that pointer variables can store addresses)

However, pointers are not just a variable that stores addresses, but are not addresses.

Is the pointer just an address?

The address is generally considered as the number of the memory unit, which is a number.

So can we use this number to operate the memory unit (use the address to complete pointer work)

1 int main()2 {3     int a = 10;4     int b;5     b =(int)&a;6     *(b) += 20;//error6     return 0;7 }

Row 3: forcibly convert the memory address of a into a number and store it to B. Then, use B to operate on a. The result is certainly not feasible.

It seems that the IP address cannot be used to complete the pointer task,

Key points:

How can I change this address to a pointer?

1 int main()2 {3     int a = 10;4     int b;5     b =(int)&a;6     *((int*)b) += 20;7     printf("%d", a);8     return 0;9 }

After running, the result is normal. The address is successfully used to operate variable.

Most of the errors mentioned above indicate type conversion errors (int * cannot be converted to int type). It seems that the compiler considers pointer as a type.

The preceding program * (B) is incorrect * (int *) B) is correct and exclusive.(Int *)It is the pointer type.

Or a pointer is composed of two parts: Data Type and address ..

 

3. In fact, pointers do not only contain address data types, but also are very important.

Because the data type determines the length of the pointer movement, for example, the char type pointer ++ address each time + 1 int type pointer address each time + 4

What's more complicated is that array pointers are very important in two-dimensional arrays. They are different in row + 1 or column + 1 operations.

 

Int main () {int arr [100]; printf ("% d \ n", arr, & arr); printf ("% d \ n ", arr + 1, & arr + 1); return 0;} running result: 9607420 96074209607424 9607820 press any key to continue...

 

Both arr and & arr in the first line are the first addresses.

The second row shows that arr + 1 memory + 4 & arr + 1 memory + 400

The arr Data Type of the address is the entire array. Therefore, + 1 is equivalent to + 100*4.

 

If the pointer has no type, reading and writing may also cause problems.

Because it is the first address, if there is no type, how do you know it is read and how many bytes are written.

 

(Original but personal opinions are for reference only)

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.