Summarized some common pointer error-prone issues (3) and pointer Common Problems
Pointer and string
Difference between NULL and NUL: the former is used to represent a special pointer (void *) 0), while the NUL is a char (\ 0) and cannot be mixed.
Character constant: single quotation marks; string: Double quotation marks;
String declaration method: literal, character array, pointer.
String Literal pool:
String Initialization
Initialize the 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 method to initialize this string is to use the malloc and strcpy functions to allocate memory and copy the literal to the string.
Char * header = (char *) malloc (strlen ("Media Player") + 1 );
Strcpy (header, "Meadia Player ");
Differences between sizeof and strlen:
Standard Input initialization string
Standard string operations
#include<stdio.h>#include<stdlib.h>#include"string.h"int main(){ 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); }
Transfer string
#include<stdio.h>#include<stdlib.h>#include"string.h"size_t stringLength(char* string){ size_t length = 0; while(*(string++)) { length++; } return length; } int main(){ 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)); }
Pointer for passing character constants
Passing parameters to an application
Returns a string.
When a function returns a string, it returns the address of the string. The focus is on returning valid addresses. You can return references to one of the following three objects: character volume, dynamically allocated memory, and local string variables.
Function pointer and string