Preprocessing command normally be placed before the start of function definition. It is used to process something before compile .
Macro definition
Macro definition normally starts with # , such as:
1 2
#define #include
The rules of using macro definition:
1 2 3 4 5 6 7 8
#define PI 3.1415926//不含参数的构造,语句结束不需要分号,#include亦然 //编译器会直接将所有的PI替换为3.1415926 #define R 3.0 #define L 2*PI*R //以上的两行也是可以的,宏定义会展开. printf("L");//格式控制输出时不置反
Pointer is a sort of variable that storages address.
Here are some basic operations of using pointer.
1 2 3 4
int a=3; int *ptr = &a;//ptr is a variable that points to the address of a *ptr;//use * to access the data at the address which ptr points to. *(ptr++);//returns *ptr,and then ptr++
Use pointer to access array:
1 2 3
int a[]={1,2,3,5,2,6,2,5} int *ptr = &a;//the address of an array is just same as the address of the first element of the array. printf("%d",*ptr++); //the ptr++ refers the pointer is going to point to the next element of the array
Use pointer to access multi dimension array;
Array
Use variable
Use pointer
Use pointer
Use pointer
a[i]
a[i]
*(a+1)
*(a+1)
*(a+i)
a[i][j]
a[i][j]
*(*(a+i)+j)
*(a[i]+j)
*(&a[i][0]+j)
An address like a (the array is a[3][3]) can’t be a value of int *p;
So,how to do that?
Use int (*p)[4] , a pointer of this type can have a value like a (the array is a[2][4]).
Pointer Array
1 2
int *p[4];//a define of a pointer array char *p[4]={"Brazil","Russia","India","China"};//a array of pointer,each points to a string.
int *p = (int*)malloc(4);; //set a space whose size is 4. int *p[4] = (int*)calloc(n,sizeof(int));//set n*size array. realloc(p,8);//resize. free(p);//delete.
Pointer to Pointer
1 2 3 4
int **p; **p=2; *p;//an address **p;//a value
Parameters to main
1 2 3 4 5 6 7 8 9 10 11 12
#include<stdio.h> #include<string.h> #include<stdlib.h> intmain(int argc,char* argv[])//argc refers string numbers and argv refers string that put in. //argv[0]=your program name { for(int i=1;argv[i]!=NULL;i++) printf("%s\n",argv[i]);
//read and write in bit fread(buffer,size,count,fp); fwrite(buffer,size,count,fp);
//examples: fread(&stu_list[i],sizeof(struct student),1,fp); fwrite(&stu_list[i],sizeof(struct student),1,fp);//you can see it as using disk as memory.
Radom R/W files
1 2 3 4
rewind(fp);//move the file flag back to the head. fseek(fp,100L,0);//move the flag to the position 100 byte away from the head. fseek(fp,100L,1);//move foward 100 byte from current position fseek(fp,-50L,3);//move back 50 byte from the end.