C and Pointers
Related basics: Allocation of memory (Tan Haoqiang Edition)
1, the address of the integer variable and floating-point type/character variable address difference? (What is the difference between an integer variable/floating-point variable)
2, int *p, pointer variable pointing to integer data.
3, through the pointer variable access to integer variables.
4. *p: the memory Unit (variable) to which the pointer variable p points
5, p = &a-->> *p = *&a
6. Using pointers as Function parameters
7, the call function, because the combination of the actual situation is a one-way "value transfer" mode, only from the parameter to the formal parameter data, the change of parameter values can not be back to the actual parameters.
8, reference to an array element can be used (1) Subscript method (2) Pointer method (accounting for small memory, running fast)
9. How do pointers improve efficiency in those areas? (Memory, run time?? )
10, if the pointer variable p already points to an element in the array, then p+1 points to the next element in the same array.
11, using the pointer variable as function parameter to accept the array address. The difference between an array pointer and an array of pointers
#include "iostream"
using namespace std;
int main ()
{
void sort (int *p,int n);
int a[5],i;
cout<< "Input array:" <<endl;
for (i=0;i<5;i++)
cin>>a[i];
cout<<endl;
Sort (a,5);
cout<< "The sorted array:" <<endl;
for (i=0;i<5;i++)
cout<<a[i]<< "";
cout<<endl;
return 0;
}
void sort (int *p,int n)
{
int i,j,k,temp;
for (i=0;i<n-1;i++)
{
k=i;
for (j=i+1;j<n;j++)
if (* (P+J) <* (p+k)) k=j;
temp=* (p+k);
* (p+k) =* (p+i);
* (p+i) =temp
}
}
12 . How many bytes does an int occupy?
13, the combination of the actual parameter and the formal parameter has 4 kinds of forms:
| Actual parameters |
Formal parameters |
| Array name |
Array name |
| Array name |
Pointer variable |
| Pointer variable |
Array name |
| Pointer variable |
Pointer variable |
14, the real parameter group name A is a fixed address, or a pointer constant, so its value is immutable; the parameter group name is a pointer variable that can be changed. a++//Grammatical error, not to be changed
15, string and pointer. String array/string variable/character pointer holds string.
The Test_3 pointer holds the string
#include <iostream>
using namespace std;
#include <string>//string variable
int main ()
{
char str1[]= "I love C + +", str2[20],*p1,*p2;
P1=STR1;
P2=STR2;
for (; *p1!= ';p 1++,p2++)
*p2=*p1;
*p2= ' ";
P1=STR1;
P2=STR2;
cout<< "STR1 is" <<p1<<endl;
cout<< "STR2 is" <<p2<<endl;
return 0;
}
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)
#include <iostream>
using namespace std;
int main ()
{
int max (int x, int y);
Int (*p) (int,int);
int a,b,m;
P=max;
cin>>a>>b;
M=p (a,b);
cout<< "Max is" << m<<endl;
return 0;
}
int max (int x,int y)
{
int z;
if (x<y)
z=y;
else
z=x;
return (z);
}
2), using pointers to functions as function parameters.
3), the function that returns the pointer value: the pointer function.
Type name * Function name (parameter 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 constant, that is, the pointer variable's point cannot be changed)
Type name *const pointer variable name
Tips: The definition must be initialized to specify its point.
The pointer variable's point cannot be changed, but the value of the pointer variable's point variable can be changed. *p2=12;//Legal
Note (the position of the const and *). When Const is behind the *, compare it with the form that defines the pointer variable that points to the constant variable.
3), a constant pointer to constants (pointer variable pointing to a fixed object, the value of the object cannot be changed).
Const base type name *const pointer variable name
18), void pointer type
The above C and pointer summary (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.