C and Pointers Summary

Source: Internet
Author: User

C and Pointers

Related basics: Allocation of memory (version rectification)

1. The address of the integer variable is different from the address of the floating-point/character-type variable? (What is the difference between an integer variable/floating-point variable)

2, int *p, pointer variable that points to the integer data.

3. Access integer variables through pointer variables.

4, *p: The pointer variable p points to the storage unit (variable)

5, p = &a-->> *p = *&a

6. Use pointers as function parameters

7, call the function, because the actual situation is the combination of a one-way "value transfer" method, only the parameter to the parameter data, parameter value changes cannot be passed back to the argument.

8, referring to an array of elements can be used (1) Subscript method (2) Pointer method (small memory, running fast)

9, pointers to improve efficiency in those areas? (Memory, run time??) )

10. If the pointer variable p already points to an element in the array, p+1 points to the next element in the same array.

11. Accept the array address with the pointer variable as a function parameter. The difference between an array pointer and an array of pointers

1#include"iostream"2  using namespacestd;3  intMain ()4  {5      voidSortint*p,intn);6      inta[5],i;7cout<<"Input array:"<<Endl;8       for(i=0;i<5; i++)9Cin>>A[i];Tencout<<Endl; OneSort (A,5); Acout<<"The sorted array:"<<Endl; -       for(i=0;i<5; i++) -cout<<a[i]<<" "; thecout<<Endl; -      return 0; -  } -   +  voidSortint*p,intN) -  { +      inti,j,k,temp; A       for(i=0; i<n-1; i++) at      { -k=i; -           for(j=i+1; j<n;j++) -          if(* (P+J) <* (p+k)) k=J; -temp=* (p+k); -* (p+k) =* (p+i); in* (p+i) =temp; -      } to}
View Code

12. How many bytes does an int occupy? Original link

13, the combination of real and formal parameters there are 4 kinds of forms:

argument parameter
array name
array name
pointer variable
pointer variable

14, real parameter group name a represents a fixed address, or a pointer constant, so its value is immutable; the shape parameter group name is a pointer variable, which can be changed. a++//Syntax error, non-change

15, strings and pointers. String array/string variable/character pointer holds string.

1 //test_3 pointer Holding string2#include <iostream>3  using namespacestd;4 //#include <string>//String Variables5  intMain ()6  {7      Charstr1[]="I Love C + +", str2[ -],*p1,*P2;8p1=str1;9P2=str2;Ten      for(; *p1!=' /';p 1++,p2++) One*p2=*P1; A(pp=' /'; -p1=str1; -P2=str2; thecout<<"str1 is"<<p1<<Endl; -cout<<"str2 is"<<p2<<Endl; -     return 0;  -}
View Code

16, functions and pointers. The entry address of a function is called a pointer to a function.

1), call function with function pointer

function type (* variable name) (function parameter list)

1#include <iostream>2 using namespacestd;3 4  intMain ()5  {6      intMaxintXinty);7      int(*p) (int,int);8      inta,b,m;9p=Max;TenCin>>a>>b; Onem=P (A, b); A       -cout<<"Max is"<< m<<Endl; -     return 0; the   }  -    -   intMaxintXinty) -   { +       intZ; -       if(x<y) +z=y; A       Else atz=x; -       return(z); -}
View Code

 2), using pointers to functions as function parameters.

3), function to return pointer value: pointer function.

Type name * Function name (argument list); int *a (int x,int y);

17.

1), pointer to constant (not allowed to modify the value of the object it points to by pointer variable)

Const type name * pointer variable name

2), constant pointer (specifies that the value of the pointer variable is a constant, that is, pointer variable pointing is immutable)

Type name *const pointer variable name

Tips: It must be initialized in the definition, specifying its point.

The pointer variable's point is immutable, but the pointer variable's value can be changed. *p2=12;//Legal

Note (the position of const and *). Const after *, compare the form of a pointer variable that defines a point to a constant variable.

3), constant pointers to constants (pointer variables point to a fixed object, and the value of the object cannot be changed).

Const base type name *const pointer variable name

18), void pointer type

C and Pointers Summary

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.