One, array
1, one-dimensional array
1 "Definition
2 "Initialization
3 "Use
4 "Storage
2, multidimensional array
1 "Definition
2 "Initialization
3 "Use
4 "Storage
3, variable length array
4. Array reference
Address delivery: Passing array names and number of elements
#include <stdio.h>
//void fun (int n,int b[])
void fun (int n,int *b)
{
int i;
for (i = 0; i < n; i++)
printf ("%d\t", B[i]);
printf ("\ n");
}
int main (void)
{
int a[5] = {1,2,3,4,5};
Fun (5,a);
return 0;
}
#include <stdio.h>
//void fun (int n,int m, b[][m])
void fun (int n,int m, int (*b) [m])
{
int i,j ;
for (i = 0; i < n; i++) {for
(j = 0; J < m; j)
printf ("%d\t", B[i][j]);
printf ("\ n")
;
}
int main (void)
{
int a[2][3] = {{1,2,3},{4,5,6}};
Fun (2,3,a);
return 0;
}
Second, the pointer
1. Concept:
In C, the address of a memory cell is called a pointer , a variable specifically used to hold an address, called a pointer variable (pointervariable), which, in the case of an understanding, sometimes addresses , Pointers and pointer variables are not distinguished, and are referred to as pointers .
2. Three key elements of the pointer:
When a pointer is initialized or assigned, the pointer has three elements, such as:
int a = 100;
int *PA = &a;
At this point, the following three expressions are available for PA:
*p represents the data that P points to
P represents the content of P
&p represents the address of the variable p in memory
3, the operation of the pointer:
1 Assign Value: =
2 "Value: *
3 Take the address of:&
4 "Addition Operation: +
Can only add one integer
5 "Subtraction Operation:-
You can subtract an integer
You can also subtract two pointers of the same type
6 "Relational Operation:> < >= <= = =!=
A comparison of the high and low addresses
4. CONST keyword
5. Multi-level pointers
In C, a variable that stores the address of a pointer variable, called a multilevel pointer variable, is referred to as a multilevel pointer.
int a = 100;
int *P1 = &a; P1 as a first-level pointer
INT**P2 = &p1; P2 is a two-level pointer
int ***P3 = &p2; P3 is a three-level pointer
In summary: The address of the n-1-level pointer variable can be stored in the N-level pointer variable.
#include <stdio.h>
int main (void)
{
int a = m;
int *P1 = &a; P1 is a first-level pointer
int **P2 = &p1; P2 is a level two pointer
int ***p3 = &p2; P3 is a level three pointer
printf ("a =%d\n", a);
printf ("*P1 =%d\n", *p1);
printf ("**P2 =%d\n", **p2);
printf ("***P3 =%d\n", ***p3);
return 0;
}
6. void key word
1 modifier function return value
Indicates that the function does not have a return value
2 parameters of the modifier function
Indicates that the function has no parameters
3 Define Pointers
Indicates that the pointer can hold any type of address, which I usually refer to as a generic pointer.
#include <stdio.h>
int main (void)
{
*p;
int a = m;
p = &a;
printf ("A:%d\n", a);
printf ("*p:%d\n", * (int*) p);
Char b = ' A ';
p = &b;
printf ("B:%c\n", b);
printf ("*p:%c\n", * (char*) p);
float C = 3.4;
p = &c;
printf ("C:%f\n", c);
printf ("*p:%f\n", * (float*) p);
return 0;
}
Iii. the relationship between pointers and arrays:
1, the pointer can point to an array, array pointers
2, the array can store multiple addresses of the same type, array of pointers
#include <stdio.h>
int main (void)
{
#if 1
int a[5] = {1,2,3,4,5};
int *b[5] = {a,a+1,a+2,a+3,a+4};
int *p = A;
int * *PB = b;
int i;
for (i = 0; i < 5; i++)
printf ("%d\t%d\n", p[i],* (P+i));
for (i = 0; i < 5; i++)
printf ("%d\t", *pb[i]);
printf ("\ n");
#else
int A[2][3] = {{1,2,3},{4,5,6}};
int (*p) [3] = A;
int i,j;
for (i = 0; i < 2; i++)
for (j = 0; J < 3; j +)
printf ("%d\t%d\t%d\n", p[i][j],* (P[I]+J), * (* (p+i) +j));
#endif return
0;
}
3, the main function with the parameter
#include <stdio.h>
/**
argc: Number of command line arguments
argv: Command line Arguments/
//int Main (int argc, char *argv[])
int main (int argc, char **argv)
{
int i;
printf ("argc =%d\n", argc);
for (i = 0; i < argc i++)
printf ("%s\n", Argv[i));
return 0;
}
Four, function
1. Basic Concepts
1 Definition of function
2 Declaration of functions
3 Call of function
4 arguments, formal parameters and their relationships
2, the function passes the parameter
1 The way of reference:
1) Value Delivery:
The argument cannot be changed in the called function
The argument type is the same as the formal parameter type
2) Address delivery
The argument can be changed in the called function
The parameter type is the pointer type corresponding to the argument type
2 According to the type of argument can be divided into the following kinds of parameters
1 argument is constant
Value Delivery
2 argument is a basic type variable
Value Delivery
Address delivery
3 argument is an array
Address delivery: Passing array names and number of elements
4 The argument is passed by the value of the pointer variable:
#include <stdio.h>
void fun1 (int *p1,int *p2)
{
int *t,b;
Cannot change the value of the argument
t = p1;
P1 = p2;
P2 = t;
You can change the data that the argument points to
B = *p1;
*P1 = *P2;
*P2 = b;
}
void fun2 (int **p1,int **p2)
{
#if 0
int *t;
Can change the value of the argument
t = *p1;
*P1 = *P2;
*P2 = t;
the data pointed to by the #else//argument can also be changed to
int b;
b = **p1;
**P1 = **P2;
**P2 = b;
#endif
}
int main (int argc, char **argv)
{
int a = 3,b = 5;
int *pa = &a, *PB = &b;
#if 0
printf ("pa =%P,PB =%p\n", PA,PB);
printf ("a =%d\t,b =%d\n", a,b);
FUN1 (PA,PB); Value passed, the PA and PB printf cannot be changed in the called Function
("PA =%P,PB =%p\n", PA,PB);
printf ("a =%d\t,b =%d\n", a,b);
#endif
#if 1
printf ("pa =%P,PB =%p\n", PA,PB);
printf ("a =%d\t,b =%d\n", a,b);
Fun2 (&PA,&PB);//address delivery, can be changed in the called function PA and PB
printf ("pa =%P,PB =%p\n", PA,PB);
printf ("a =%d\t,b =%d\n", a,b);
#endif return
0;
}
3 returns the function's address 4) returns the value character type pointer
#include <stdio.h>
int fun1 (void)
{
int x = 123;
return x;
}
char * fun2 (void)
{return
' Hello World ';
}
int *fun3 (int n,int a[])
{
int i;
for (i = 0; i < n; i++)
a[i] *= 2;
return A;
}
void Fun (void)
{
printf ("Hello world\n");
}
typedef void (*type) (void);
void (*fun4 (void)) (void)
TYPE fun4 (void)
{return
fun;
}
Char *fun5 (void)
{
//char p[] = "Hello World"; P cannot return
char *p = "Hello World"; P can be returned to return
p;
}
int main (int argc, char **argv)
{
#if 1
char *str;
str = FUN5 ();
printf ("%s\n", str);
#else
Void (*p) (void);
p = fun4 ();
P ();
int arr[5] = {1,2,3,4,5};
int *p,i;
for (i = 0; i < 5; i++)
printf ("%d\t", Arr[i]);
printf ("\ n");
p = fun3 (5,arr);
for (i = 0; i < 5; i++)
printf ("%d\t", Arr[i]);
printf ("\ n");
#endif return
0;
}
Universal line makefile, in the current directory needs to generate more than one executable file is direct make, and based on time stamp, respectively, one by one generate the corresponding file name executable
CC = gcc
cflags =-wall-g-o0
assigns all *.c filenames under the current directory to src
src = ${wildcard *.c} When executing make #wildcard
#patsubst A parameter that matches the file name in the third parameter, and then converts it to the format of the second parameter
OBJS = ${patsubst%.c,%,$ (SRC)}
#all为伪目标
all:$ (OBJS)
%:%. C
$ (CC)-o $@ $^ clean
:
$ (RM) $ (OBJS)