And finally to the essence of the place, this is indeed a bit ignorant, always I feel it's too much trouble, but also unhappy that says, still miss py or java, but there is no way, or to keep learning.
First, the operator &
- scanf("%d" , &i); 里的&
- Get address of a variable, its operand must be a variable
- Int address whether the size of the same depends on the compiler
#include <stdio.h>
int main(void)
{
int i = 0;
printf("0x%x\n", &i);
// 0x62fe4c
return 0;
}
& Can not take the address
& Can not take the address of something without an address
- &(a+b) ?
- &(a++) ?
Second, Pointer
- If we can get the address of a variable of a transfer function, you can access this variable through the address within that function?
- scanf("%d" , &i);
- scanf () prototype should be like? We need a parameter save another variable address, how to express variable can hold the address
pointer
Is the variable that holds the address , * the p-
// p是一个指针,现在把i的地址交给了p
int i;
int* p = &i;
// 下面两种形式一样,p是一个指针,而q是一个普通的int变量
int* p , q;
int *p , q;
Pointer variable
- The value of the variable is the memory address
- The value of common variable is the actual value
- The value of the address pointer variable is a variable actual value
As indicator parameter
- void f(int *p);
- When it is invoked get the address of a variable
- int i = 0;f(&i);
- This pointer can be accessed through the function inside outside this i
#include <stdio.h>
void f(int *p);
int main(void)
{
int i = 6;
printf("&i=%p\n", &i);
f(&i);
return 0;
}
void f(int *p)
{
printf(" p=%p\n", p);
}
// 可以看到这里获取的地址是相同的
// &i=000000000062FE4C
// p=000000000062FE4C
Access variables on the address that *
- * Is a unary operator, the address value used to access variables indicated by the pointer
- Value may be a right or left-hand value
- int k = *p;
- *p = k + 1;
* Left value was called lvalue
- Because the number appears in the left side of the assignment is not a variable, but the value is the result of the calculation of expression
- a[0] = 2;
- *p = 3;
- Is a special value, so called lvalue
#include <stdio.h>
// 声明两个函数
void f(int *p);
void g(int k);
int main(void)
{
int i = 6;
printf("&i=%p\n", &i);
f(&i);
// 此时i的值已经发生了变化
g(i);
return 0;
}
// 传入的是地址
void f(int *p)
{
printf(" p=%p\n", p);
printf("*p=%d\n", *p);
*p = 66;
}
// 传入的普通int
void g(int k){
printf("k=%d\n", k);
}
// &i=000000000062FE4C
// p=000000000062FE4C
// *p=6
// k=66
Third, the use of pointers
A pointer scenarios
Exchange of the value of two variables
#include <stdio.h>
void swap(int *pa , int *pb);
int main()
{
int a = 5;
int b = 10;
swap(&a , &b);
printf("a=%d , b=%d\n", a , b);
return 0;
}
void swap(int *pa , int *pb){
int t = *pa;
*pa = *pb;
*pb = t;
}
Scene Two pointer Application
- Function returns a plurality of values, some values can only be returned by the pointer
- Incoming parameters actually need to save the results back to the variables
#include <stdio.h>
void minmax(int a[] , int len , int *min , int *max);
int main(void)
{
int a[] = {1,2,3,4,5,6,7,8,9,12,13,14,15,34,35,66,};
int min , max;
minmax(a , sizeof(a)/sizeof(a[0]) , &min , &max);
printf("min = %d , max = %d \n", min , max);
return 0;
}
void minmax(int a[] , int len , int *min , int *max)
{
int i;
*min = *max = a[0];
for (i = 0; i < len; i++)
{
if (a[i] > *max)
{
*max = a[i];
}
if (a[i] < *min)
{
*min = a[i];
}
}
}
Two pointer scenarios b
- Function returns the status of the operation, the result returned by the pointer
- Common routine is to return a special function does not belong to a value in the range indicates an error
- -1 or 0
- But when values are valid any possible results, you have to separate returns
#include <stdio.h>
// 如果成功就返回1,否则就是0
int divide(int a , int b , int *result);
int main(void)
{
int a = 5;
int b = 2;
int c;
if (divide(a,b,&c))
{
printf("%d/%d = %d\n", a, b, c);
}
return 0;
}
int divide(int a , int b , int *result)
{
int ret = 1;
if ( b== 0)
{
ret = 0;
}else{
*result = a/b;
}
return ret;
}
Pointer common errors
Defines a pointer variable, there is no point to any variable, you begin to use
Fourth, pointers and arrays
What passed into the function of the number of make up?
- Array function parameters of the table pointer is actually
- sizeof(a) == sizeof(int*)
- But [] array for operation with operator
The following four kinds of function prototype is equivalent to
int sum(int *ar , int n);
int sum(int* , int);
int sum(int ar[] , int n);
int sum(int[] , int);
Array variable is a special pointer
Array Variables expressing itself address,
- int a [10]; int * p = a; // & fetch address without using
- However, the expression of cell array is a variable, & need to take address
- a == &a[0]
[] Operator can make an array, a pointer can be done
- p[0] <===> a[0]
* Operator can make the pointer, the array may be made
- *a = 25
Const array variable is a pointer, it can not be assigned
V. pointer and const
- Once represent the address of a variable, you can not point to other variables
// q内写的地址不能被改变
int *const q = &i;
*q = 26; // ok
q++; // error
- Not represented by this pointer to modify that variable (variable does not become such that const)
const int *p = &i;
*p = 26; // error (*p是不能变的)
i = 26; // ok
p = &j; // ok
- Understand the meaning of the following?
const int * p1 = &i;
int const * p2 = &i;
int *const p3 = &i;
That judgment is const const logo is in front of or behind the
front two p can not be modified , just like the second case
const array
const int a[] = {1,2,3,4,5,6,};
- Const array variable is a pointer to the already, const here means that each element of the array are const int
- It must be initialized by assignment
Protection array of values
- Because the transfer function of the array is an incoming address, so that the internal function can modify the value of the array
- In order to protect the array is not destroyed function, it may be provided as a parameter const
int sum(const int a[] , int length);
Six pointer arithmetic
1 + 1 = 2? So pointer plus 1 equals what it
#include <stdio.h>
int main(void)
{
char ac[] = {0,1,2,3,4,5,6,7,8,9,};
char *p = ac;
printf("p =%p\n", p);
printf("p + 1 =%p\n\n", p+1);
// p =000000000062FE30
// p + 1 =000000000062FE31
// 相差了1
int ai[] = {0,1,2,3,4,5,6,7,8,9,};
int *q = ai;
printf("q =%p\n", q);
printf("q + 1 =%p\n\n", q+1);
//q =000000000062FE00
//q + 1 =000000000062FE04
// 相差了4
return 0;
}
We find that, in the char, a difference of 1, a difference of 4 in an int, how is this going?
sizeof(char) = 1 , sizeof(int) = 4
Pointers 1 to a plus operation, meaning that it should move to the next cell to
int a[10];
int *p = a;
*(p+1) --> a[1]
If the pointer is not pointing to a continuous distribution of space, such as an array, then this operation does not make sense
The pointer arithmetic can be
- Pointer to add, subtract, an integer (+, + =, -, - =)
- Gradual increase and decrease (+ / -)
- Subtracting two pointers
#include <stdio.h>
int main(void)
{
char ac[] = {0,1,2,3,4,20,6,7,8,9,};
char *p1 = &ac[5];
char *p2 = &ac[4];
printf("p1 - p2 = %d\n" , p1 - p2);
printf("*p1 - *p2 = %d\n" , *p1 - *p2);
// p1 - p2 = 1 也就是 5 -4
// *p1 - *p2 = 16 也就是 20 - 4
int ai[] = {0,1,2,3,4,5,6,7,8,18,};
int *q1 = &ai[9];
int *q2 = &ai[2];
printf("q2 - q1 = %d\n", q2 - q1);
printf("*q2 - *q1 = %d\n", *q2 - *q1);
// q2 - q1 = -7 也就是 2 - 9
// *q2 - *q1 = -16 也就是 2 - 18
return 0;
}
*p++
- Data indicated that p be removed, after the bin is moved to the next the way to a position p
- * Although high priority, but not high ++
- Commonly used in the operation of the array type continuous space
- In some cpu, which can be directly translated into an assembly instruction
char ac[] = {0,1,2,3,4,20,6,7,8,9,-1};
char *p = &ac[0];
// 普通方法遍历数组
// int i ;
// for (i = 0; i < sizeof(ac)/sizeof(ac[0]); i++)
// {
// printf("%d\n", ac[i]);
// }
// 普通指针遍历数组
// for (p = ac; *p != -1; p++)
// {
// printf("%d\n", *p);
// }
// printf("---------------\n");
// 使用*p++,快
while(*p != -1){
printf("%d\n", *p++);
}
0 Address
- Of course, you have the memory address 0, address 0 but usually not just a touch of address
- So you should not have a pointer to a value of 0
- So you can use 0 address to represent special things
- The returned pointer is invalid
- Pointer not really initialized (initialized to 0)
- NULL is a predetermined symbol definition, represents the address 0
Pointer types
- No matter what type of points, the size of all the pointers are the same, because all address
- However pointers pointing to a different type of assignment is not directly to each other
- Just to avoid using the wrong hands
What to do with a pointer
- When used as a parameter data need to pass a large
- After passing an array of arrays to do the operation
- Function returns more than one result
- We need to change more than one variable with function
- Dynamic Application Memory
Seven, dynamic memory allocation
Input data
- If the input data, to tell you the number, then enter, each data to be recorded
- C99 can do with the size of the array defined variables, C99 before it?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int number;
int *a;
int i;
printf("请输入数量:\n");
scanf("%d" , &number);
// 动态内存分配用malloc,此时a就相当于数组
a = (int*)malloc(number*sizeof(int));
for (i = 0; i < number; i++)
{
scanf("%d" , &a[i]);
}
for (i = number - 1; i >=0; i--)
{
printf("%d\n", a[i]);
}
// 最后释放空间
free(a);
return 0;
}
malloc function
#include <stdlib.h>
void* malloc(size_t size);
- Size in bytes of space to malloc apply as a unit, what type need to write what type inside sizeof
- Returned result is void *, you need to type to type their needs
- Finally free up space
(int*)malloc(n*sizeof(int));
No room for?
- If the application fails to return 0, otherwise known as NULL
- Your system can give you much space?
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
void *p;
int cnt = 0;
while((p = malloc(100 * 1024 *1024))){
cnt ++ ;
}
printf("分配了%d00MB的空间\n", cnt);
return 0;
}
// 分配了27200MB的空间
free()
- The application was returned to space systems
- You can also apply for the first address space
- A corresponds to a free malloc