Pointers and strings
Null and NUL difference: The former is used to represent a special pointer ((void*) 0), and Nul is a char (), not mixed.
Character constants: single quotes; strings: double quotes;
How a string is declared: literal, character array, pointer.
string literal pool:
String initialization
Initialize char array: char header[]= "Media Player";
strcpy function Initialization Array
Char header[13];
strcpy (header, "Meadia Player");
2. Initialize the char pointer
char *header; a common way to initialize this string is to use the malloc and strcpy functions to allocate memory and copy the literal to a string.
Char *header= (char*) malloc (strlen ("Media Player") +1);
strcpy (header, "Meadia Player");
The difference between sizeof and strlen:
Standard input initialization string
Standard string manipulation
#include <stdio.h>#include<stdlib.h>#include"string.h"intMain () {Char* error="ERROR:"; Char* errormessage="Not enough memory"; Char* Buffer= (Char*)malloc(strlen (Error) +strlen (errormessage) +1); strcpy (Buffer,error); strcat (buffer, errormessage); printf ("%s", error); printf ("%s\n", errormessage); }
Passing strings
#include <stdio.h>#include<stdlib.h>#include"string.h"size_t Stringlength (Char*string) {size_t length=0; while(*(string++) ) {length++; } returnlength;} intMain () {Char* error="ERROR:"; Char* errormessage="Not enough memory"; Char* Buffer= (Char*)malloc(strlen (Error) +strlen (errormessage) +1); strcpy (Buffer,error); strcat (buffer, errormessage); printf ("%s\n", buffer); printf ("%s\n", error); printf ("%s\n", errormessage); printf ("%d\n", buffer); printf ("%d\n", Stringlength (buffer)); }
Passing a pointer to a character constant
Passing parameters to an application
return string
When a function returns a string, it returns the actual address of the string. The point is how to return a valid address that can return a reference to one of the following three objects: the character amount/dynamically allocated memory/local string variable.
function pointers and strings
Summarizes some common problems with pointers error-prone (iii)