Simple summary of the concepts of various types of pointers in C language _c language

Source: Internet
Author: User
Tags arithmetic constant define function modifier what array

C language has a lot of pointers on the use of the pointer is also the soul of C language, and C language also has a lot of pointers in the concept, here to learn and summed up some know the concept.

constant Pointers:
first, it's a pointer, and the constant is just the attribute used to modify the pointer. The definition is as follows:

Char const * CP; 
Char a= ' a '; 

How to identify it? The right combination takes precedence, first * First, so this CP variable is a pointer, then a const modifier *, so this is a constant pointer. A pointer to a constant.

cp=&a; Normal syntax 
*cp=a//error syntax, because the value it points to is a constant 


Pointer constants:
first it is a constant, and the pointer is used to modify the constant, which is the value of the constant as a pointer address.

 
char * const CP; 
Char a= ' a '; 

How to identify it? The right combination takes precedence, first the const first, so the CP variable is a constant, then the * modifies the const, so this is a pointer constant.

cp=&a; Error syntax, because its address is a constant
*cp=a;//correct, the address is pointing to a common character


Array of pointers:
first, it's an array of pointers that are used to decorate the contents of the array, and what array to represent: the array that holds the pointer

Char *arr[3] = {"1", "123", "345"}; 

How to recognize because [] has a precedence greater than *, so it is first defined as an array and then modified by * To modify the number

printf ("arr0%c\n", *arr[0]); 
printf ("arr1%s\n", arr[1]); 


Array Pointers:
first it is a pointer, and the array is a pointer to the array, which is the decorated pointer.

char (*P) [3];  The declaration cannot initialize 
char arr[3] = {' 1 ', ' 4 ', ' 7 '}; 
p=&arr; Point to the first address of the array, while the type of the pointer is of type char * [3], that is, the number of sizeof (char [3]) three bytes after Operation plus 1 

How to identify: because this time you added a display priority, so this time, first a pointer, then [] decorate the pointer

printf ("%c\n", (*p) [0]);  Take the first address of the ARR, and then take the array content from this address 
printf ("%c\n", (*p) [1]); 
printf ("%c\n", (*p) [2]); 
printf ("%c\n", * ((char*) p+0)); Convert first to char pointer, then 
printf ("%c\n", * ((char*) p+1)); 
printf ("%c\n", * ((char*) p+2)); 
printf ("%c\n", ((char*) p) [0]); First converts to a char pointer, then the value of the array, and the first similar to 
printf ("%c\n" ((char*) p) [1]);  
printf ("%c\n", ((char*) p) [2]);  

function pointers:
first it's a pointer, and the function is a pointer to a function that modifies the pointer.

char (*func) (void); Define function pointer 
char test (void)  
{return 
' A '; 
} 
func = test;  Initialize assignment 
printf ("Test address:%p\n", test); 
printf ("Func Address:%p\n", func); 
char ch = func ();  Invoke 
printf ("%c\n", ch); 

How to recognize, as with array pointers, the precedence of (), so this definition is first a pointer, and then the description of the pointer, that is, a pointer to a function, which points to the function is also specified: that is, the return of the character type, do not need to pass the argument


pointer function:
first, it's a function, the return type of the pointer modifier function, a function that returns a pointer

Char *func (void); 

How to identify, because there is no expansion of the arc, so * priority does not have the right of the expansion of the high priority, so first set a function, * only to modify the return value of the

Char *func (void) { 
  char *str = ' Test '; 
  return str; 
 } 
 void Main () { 
  char *test = func ();  
  printf ("%s\n", test);  
} 



Structure Body pointer:
of course it was a pointer, but it was just pointing to the structure. Therefore, the structure body pointer.


pointer structure Body:
pointer structure, in fact, there is no need for this concept, nothing more than a structure with pointers as children.


pointer type conversions:
pointer type conversion is an interesting thing, you can convert an int pointer to a char type, and then convert a pointer of type char to an int, just like a conversion between a normal character and an int type. But the value of the pointer has not changed, the only thing that changes is the step of the pointer, that is, the calculation of the pointer operation. When it is a char pointer, its unit of operations is 1 bytes in 1 units of operation, and when the int pointer is usually 4 bytes in 1 units of operation.

Pointer arithmetic:
according to the above pointer type conversion introduction, different pointer types are evaluated differently when they are computed, except that the number of bytes in the step is different, and that the size of several bytes is determined by their pointer type, and the pointer length to char is 1. The usual pointer operation has the addition and subtraction of pointers and numbers, subtraction of the same type of pointer, and if it points to the same number of groups, it does not make much sense. Similarly, pushing different types of pointers makes more sense and even makes an error.
Here is an example of a pointer arithmetic that exchanges two variable values without taking advantage of additional variables
After all, the new keyword or application of additional memory, although no application variables, change the soup did not dressing

int *a,*b;   
a=new Int (a);    Assign a value to the pointer 
b=new Int ();    a=0x00030828,b=0x00030840 
a= (int*) (b-a);    a=0x00000006 
b= (int*) (B-int (a));  b=0x00030828 
a= (int*) (B+int (a));  a=0x00030840 

Just swapping variables can also be:

int a = 4; 
int b = 5; 
 
* (((char*) &a) +1 = * ((char*) &b); 
* ((char*) &b) =* ((char*) &a); 
* ((char*) &a) =* (((char*) &a) +1); 
* ((char*) &a +1) = 0; 


pointer Parameters:
The pointer parameter means that the pointer is passed as a parameter of the function, because the C language only supports one-way values, and the return value can only be a value type, you want to return multiple content from a function or have a common data manipulation area with the function body, then consider the way to pass pointer arguments. Passing a pointer is also a value, except that the value is the address that the pointer points to, that is, an int integer. By passing an address, you can manipulate and share data to a common area.

pointer pointer:
the pointer to the pointer is the pointer to the pointer, as well as a pointer to the pointer pointer, but the general human mind can do two, three-level pointer is very good. The main point here is to expand the correlation between the pointer and the multidimensional array. The processing of multidimensional arrays is decomposed from individual to general. The individual is definitely using a two-dimensional array for example, which can be extended to multidimensional arrays.

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.