Dynamic memory allocation:
malloc function:
- To add header file #inclde < stdlib. H >
- Format: void*malloc (size_t size);
- The size of the space requested from malloc is in bytes
- The returned result is void, which requires type transformation For example, a = (int *) malloc (variable or number * number);
#include<stdio.h> #include<stdlib.h> int main() { int b=7; int *a; int i; //You can use a as an array a=(int*)malloc(b*sizeof(int)); //input for(i=0;i<7;i++){ scanf("%d",&a[i]); } //output for(i=0;i<7;i++){ printf("%d\n",a[i]); } //Free up memory space free(a); for(i=0;i<7;i++){ printf("%d\n",a[i]); } return 0; }
When there is no space, the application fails and returns 0 or NULL.
free function:
- Format: free (variable): Prototype: void free (void * variable); Note that this variable cannot release the pointer from int *p
- free() frees up the memory space allocated by malloc(), calloc(), realloc() so that other programs can use it again.
- The applied space should be returned in the end; if it is not returned, memory garbage and memory vulnerabilities will be generated
- Only the first address of the requested space can be returned For example, P applied to malloc for space, and then p + +, free (P) only released P, but could not release P++
character string:
- Char variable, which can be stored in the character char a = 'a';
- A string of characters ending with 0 or '\ 0' (integer 0). Note that it is different from '0'. 0 marks the end of the string and is not a part of the string. The length of the calculated string does not include 0
- Strings exist in the form of arrays and are accessed more in the form of pointers, arrays or pointers
- string.h has many functions to handle strings
- You can traverse strings through arrays
- You cannot operate on a string with an operator
String variable:
- char *str="Hello";
- char word[]="Hello";
- char line[10]="Hello"; If it takes six bytes, the system will automatically generate an ending 0 and count one byte
String constant:
- char *s="Hello World"; const char *s="Hello World";
- s is a pointer initialized to point to a string constant to spoof
- Because it is actually const type, the place where this constant is located is a code segment, which is read-only and cannot be written
- Two string constants of the same content have the same address
- If you need to modify the string, you should use the array: char s [] = "Hello world";
#include<stdio.h> int main(void) { int i=0; const char *s="Hello World"; const char *s2="Hello World"; char *s3="Hello"; printf("%p",&i); //&i=000000000065FE04 //s=0000000000429000 //s2=0000000000429000 / / code segment, read-only //s3=000000000042900C }
Array: this string is here, readable and writable
-
Space is automatically reclaimed as a local variable
Pointer: this string doesn't know where it is. It's read-only
- Processing parameters
- Dynamic allocation of space malloc
String assignment:
char *t="title"; char *s; s=t;
No new string is generated, but the pointer s points to the string referred to by T. the operation on S is done on t
String input / output:
char string[8]; scanf("%s",string); printf("%s",string);
- scanf reads a word (until a space, tab, or carriage return) The PS: gets function can read spaces, but it is not safe
- scanf is not secure because the length of the content to be read in is not known
Unsafe input, solution:
- The number between% and s indicates the maximum number of characters allowed to be read. This number is one smaller than the size of the array (\ 0 occupies the last array), % S is changed to% 7s, indicating a maximum of 7 characters
- If the input exceeds the number of characters allowed to be read, the excess characters will be counted into the next scanf
Common errors:
char *i;
scanf("%s",i);
- If you think char * is a string type, you can use it directly
- Because i is not initialized to 0, it is not necessary to make an error every time
#include<stdio.h> #include<stdlib.h> int main(void) { char *ch; ch = (char *)malloc(sizeof(char)*100); scanf("%s",ch); puts(ch); return 0; }
Empty string:
char buffer[100]=" "; The length of this array is 100 and its contents are empty
char buffer[ ]=" "; The length of this array is only 1
2D character array:
char a[ ] [ ];
char *a[ ];
Program parameters:
int main(int argc,char const *argv[ ])
-
argc is the total number of parameters on the command line
argv [] is argc parameters, of which the 0th parameter is the full name of the program, and the subsequent parameters are user input parameters followed by the command line
-
argc and argv are passed to the program through the command line window.
-
When the program starts (), it carries parameters to the program instead of typing things to the program during operation. At this time, it is necessary to use the main function with parameters (int argc, char *argv []).
-
putchar:
- int putchar(int c);
- Write a character to standard output
- Returns a few characters to write. EOF (- 1) indicates write failure
getchar:
- int getchar(void);
- Read a character from standard input
- The return type is int to return EOF (- 1)
- Distinguish between scanf, which takes a string of characters or numbers as a whole, and getchar reads in characters or numbers one by one
#include<stdio.h> int main(int argc,char const *argv[]) { int ch; while((ch=getchar())!=EOF){ putchar(ch); } printf("EOF\n"); return 0; } //Any character or number written will be output intact //Under windows, CTRL+Z will output EOF
Role of EOF:
- When the terminal has character input, the EOF generated by Ctrl+Z is equivalent to ending the input of the line, which will cause a new round of input of getchar();
- When there is no character input at the terminal, or when getchar() reads a new input, enter Ctrl+Z. at this time, the EOF generated is equivalent to the end of the file, and the program will end the execution of getchar().
string.h:
strlen
- size_t strlen(const char *s); Strlen (array name);
- size_t indicates the object type, const ensures that the variable is not changed
- Meaning: returns the string length of s (excluding the ending \ 0)
-
#include<stdio.h> #include<string.h> int f(const char*s) { int cnt=0,idx=0; while(s[idx]!='\0'){ idx++; cnt++; }//The logarithm Group s starts counting until it is read into the last \ 0 of the character array return cnt; } int main(int argc,char const *argv[]) { char line[]="Hello"; printf("strlen=%lu\n",strlen(line)); printf("strlen=%lu\n",f(line)); //%lu indicates the output of an unsigned long integer (long unsigned) printf("sizeof=%lu\n",sizeof(line)); return 0; } // Results: strlen = 5 strlen = 5 sizeof = 6
strcmp
- int strcmp(const *s1,const *s2); strcmp(s1,s2);
- Compare two strings and return:
- 0: s1==s2
- 1: s1>s2
- -1: s1<s2
- Compare the ASCii code size of the corresponding characters between the two arrays and subtract to obtain the result
- Three methods of comparing character sizes
#include<stdio.h> #include<string.h> //This function does not require string.h int mycmp(const char *s1,const char *s2) { // int idx=0; // whlie(s1[idx]==s2[idx]&&s1[idx]!='\0'){ // inx++; // } while(*s1==*s2&&*s1!='\0'){ s1++; s2++; } return *s1-*s2; } int main(int argc,char const *argv[]) { char s1[]="abc"; char s2[]="abc"; printf("%d\n",mycmp(s1,s2)); printf("%d\n",strcmp(s1,s2); }
strcpy
- char*strcpy(char*restrict dst,const char *restrict src);
- Copy the src string to dst from back to front. restrict means that src and dst do not overlap (c99)
- If there is a return value, it returns dst and can participate in other function operations
- Copy a string
char*dst=(char*)malloc(strlen(src))+1); //+1 is because strlen does not include the ending \ 0 character strcpy(dst,src);
char*f(char*dst,const cahr*src) { int idx=0; while(src[idx]){ dst[idx]=src[idx]; idx++; } dst[idx]='\0'; return dst; }
char*f(char*dst,const cahr*src) { char*ret=dst; // while(*dst++=*src++) // ; // *dst='\0'; while(*src!='\0'){ // *dst=*src; // dst++; // src++; *dst++=*src++;//Take value first and then increase automatically } return ret; }