------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
"Input and output of strings"
1. String output:
%s-------output characters from the given address until it encounters a stop;
printf ("%s\n", &a[3]);
printf ("%s\n", a);
Char a[]= "hellowo\0rd!";
2. String input:
Receives a string from the keyboard, stored in an array of characters with the first address of a
scanf ("%s", a);
printf ("%s\n", a);
If you use%s to enter attention to whitespace issues
If the input string has a space, then the character after the space cannot be received, the input encounters a space to end;
The length of the input string is less than the length of the array and will be out of bounds;
3. Use of the string terminator:
Terminator;
The Terminator can be used to determine the length of the character;
No terminator%s could not be stopped;
Improper use of the, will only print part;
4, the string length calculation method :
1) int n=sizeof (a), not affected by the effect of the;
2) The string function calculates the length strlen (str1);
3) as the condition of the "," to judge, encountered in the end of the ";" is the length of the string;
5. The difference between a character array and an ordinary array:
There is no essential difference:
Char[],int[]
The array name is a pointer address, a constant, and the element has a type;
"String correlation function"
1. String function:
#include <stdio.h> standard output input function header file
1) puts () outputs a string
Char a[]= "Hello, world!";
Puts (a);
Line wrap, () inside is a character array address;
2) gets () Enter a string: gets (b); inside is the array name address;
Warning:this program uses gets (), which is unsafe.
There is an unsafe warning, there is a cross-border problem, you can receive the space, try not to cross the border;
#include <string.h> string function header file
3) strcat () connect a string
Char a[100]= "Hello", b[]= ", world!";
Strcat (A, b);
Puts (a);
Connect B to the back of A; B to cover the last character of a.
You have to add a header file, #include <string.h>
The lengths of A and B to become new a,a should be large enough to keep the a+b at least.
4) strcpy; string copy function;
strcpy (A, b); Copy the string of B into a string, where a is large enough and may be out of bounds;
5) stecmp; string comparison function;
strcmp (A, b); Compares the size of two strings;
Rules: Compare their ASCII values and compare the order in which they appear in the dictionary
The process of comparison: one by one;
Return value: Greater than 0--a>b, less than 0---a<b, equal to 0---a=b; the value is the number of two string elements of the difference;
6) The length calculation function of the strlen string;
int I=strlen (a); does not contain \ n, array-length-independent, and character-element-related
Pointer
1. Pointer concept:
The memory unit number is the address, according to the address can find the contents, all call this address pointer; Pointer is a memory unit number address;
A memory cell address is a pointer, where the data is the contents of the unit;
2, the advantages of pointers:
1) provide a flexible means of modifying variables for functions;
2) Let the function have multiple return values;
3) can improve the efficiency of sub-procedures
4) Providing support for dynamic data structures
3. How to access variables
1) Direct access gets the variable value by the variable name;
2) Indirect access gets the value of the variable by the pointer (address);
4. Pointer variable:
The variable that holds the pointer, the variable that holds the address, and a variable that holds the pointer
The pointer is an address and is a constant;
A pointer variable is one that holds an address and is a variable;
5. Define pointer variables:
Three parts:
1) pointer type description, which defines the variable as a pointer variable
2) pointer variable name
3) Variable value (pointer)
General form:
Type declarator * variable name; Type declaration is the type of storage that represents the pointer to a variable
Int *a;
6, the definition of matters needing attention:
1) "*" must have, if not the equivalent of defining a common variable;
2) Pointer variables can not be used to hold values and characters, only address;
3) Store the address of the storage data type to be consistent;
4) The same as the variable also has a global and local points;
"Pointer variable initial"
1. Initialize:
1) Define the initialization at the same time:
Full initialization:
int i=0;
int *a1=&i,*a2=&i; The A1A2 all pointed to I;
Partial initialization
int *a1=&i,*a2;
2) First define after initialization:
int i=0;
int *a;
a=&i;
3) If a pointer is defined, but you do not know who to point the pointer to
int *a1=null;null is empty 0;
int *a1=0; nothing points;
Common errors:
int *a1=1000; The address should be stored;
The direction of the pointer can be changed;
int *a1=*a2;
If no initialization is a wild pointer;
2. Use * To obtain the contents of the corresponding storage area of the pointer:
&: take address symbol;
*: pointer operation symbol;
int i = 10;
int *a;
A = &i;
int b = *a;
* pointer variable, function: Gets the memory that the pointer variable points to the stored content
Two Uses of "*":
1) used to define a pointer variable: int *a1=&i
2) Get the value int b = *a;
3) Set the pointer variable to the contents of the storage space
int i=10;
int *a=&i;
*a=100;
If a pointer variable is not initialized, the pointer variable holds a garbage number, which is called the wild Pointer, and the pointer variable
Two number exchange:
1#include <stdio.h>2 3 voidChangeint*P1,int*p2);4 5 voidChangeint*P1,int*p2) {6 7*P1 = *p1^*P2;8 9*P2 = *p1^*P2;Ten One*P1 = *p1^*P2; A - } - the intMainintargcConst Char*argv[]) { - - intA =Ten, B = -; - + int*P1 = &a, *P2 = &b; - + Change (P1, p2); A atprintf"a=%d\nb=%d\n", *p1,*p2); - - return 0; - -}
3, the common use of pointers to the scene:
1) indirectly access the variables in the caller in the function:
2) allow the function to have multiple return values;
"Level Two pointer"
1, Level Two pointers:
If a pointer variable holds the address of another pointer variable, the pointer variable is a pointer variable that points to the pointer. Also for level two pointers;
int*p=&a;
int**p1=&p;
* First-level pointer,* * level two pointer,* * * three pointer;
2. The pointer distinguishes the type;
Under the same compiler, the space occupied by a pointer is determined and occupies 8 bytes in the system.
Defines what type of pointer is pointing to what type of variable;
Black Horse Programmer---C base 9 "string Input Output" "String correlation Function" "pointer" "Pointer variable initial" "two level pointer"