当前位置:网站首页>Hello C (VI) -- pointer and string

Hello C (VI) -- pointer and string

2022-06-24 23:42:00 Tianshan old demon

One 、 String Introduction

1、 String declaration

C There is no string concept in the language , Simulate a string through a special character array ,C The string in the language is ’\0’ Array of characters at the end .

There are three ways to declare strings : Literal 、 A character array 、 Character pointer .

A string literal is a sequence of characters enclosed in double quotation marks , In essence, it is a character array stored in the global read-only storage area of the program , Commonly used for initialization , In the string literal pool , A character literal is a character enclosed in single quotation marks . The compiler automatically adds... To the string literal ’\0’ Terminator .

A string literal pool is an area of memory allocated by a program , It is used to save the sequence of characters that make up a string . In modern compilers, for example GCC Will optimize string literals , When a string literal is used multiple times , Usually only one copy is saved in the string literal pool , Generally, string literals are allocated in read-only memory , It's immutable , But when the compiler option on literal pool is turned off , String literals can produce multiple copies , Each copy has its own address .

String is based on ASCII character NUL Ending character sequence . Strings are usually stored in arrays or in memory allocated from the heap . Not all character arrays are strings , The character array may not NUL character .

The length of the string is the length of the string except NUL Number of characters other than characters .

A character constant is a sequence of characters enclosed in single quotation marks , It usually consists of one character . The length of the characters is 1 Bytes , The length of a character literal is 4 Bytes .sizeof(char)= 1,sizeof(‘a’) =  4.

A character array is an array , The value of each element can be changed . The string pointer points to a constant string , It is stored in the static data area of the program , Once defined, it cannot be changed . This is the most important difference .

2、 String initialization

The method used to initialize the string depends on whether the variable is a declared bit character array or a character pointer , The memory allocated for a string is either an array or a block of memory pointed to by a pointer .

Initializes an array of characters :

char buffer[] = “hello world”;// The length of the string is 11, Literal requirement 12 Bytes 
char buffer[12];
strcpy(buffer, “hello world”);

Initialize character pointer :

char *buffer = (char *)malloc(strlen(“hello world”) + 1);
strcpy(buffer, “hello world”);

Two 、 Pass string

Arguments are often declared as character pointers in functions .

1、 Pass a simple string

Pass string literals directly :

char buffer[12];
strcpy(buffer, “hello world”);

Passing an array of characters :

char src[] = “hello world”;// The length of the string is 11, Literal requirement 12 Bytes 
char dest[12];
strcpy(dest, src);

To prevent the incoming string from being modified , The passed formal parameter can be declared as const.

const char src[] = “hello world”;// The length of the string is 11, Literal requirement 12 Bytes 
char dest[12];
strcpy(dest, src);//char *strcpy(char *dest, const char *src);

2、 Pass the string that needs to be initialized

Function returns a string that requires function initialization , So you need to pass the buffer to the function .

A、 The address and length of the buffer must be passed

B、 The caller is responsible for releasing the buffer

C、 Functions usually return a pointer to a buffer

char *pfun(char *buffer, int size)
{
    xxxxx;
    return buffer;
}

3、 Pass parameters to the application

int main(int argc, char **argv)
{
    return 0;
}

Through the application's entry function main You can pass parameters to an application

3、 ... and 、 Return string

When the function returns a string, it actually returns the address of the string , The returned address must be legal .

1、 Return literal address

char *returnstring(int code)
{
    xxx;
    return “hello world”;
}

2、 Return the address of dynamically allocated memory

Dynamically allocate string memory from the heap in the function , The return address .

char *blanks(int num)
{
    char *buffer = (char *)malloc(num + 1);
    strcpy(buffer, “hello world”);
    xxx;
    retrurn buffer;
}

After use , Function callers must free memory , Otherwise, memory leakage will occur .

3、 Returns the address of a local string

It is not advisable to return the address of a local string , The memory of the local string will be overwritten by other stack frames .

char *blanks(void)
{
    char buffer[] = "hello world";
    return buffer;
}

原网站

版权声明
本文为[Tianshan old demon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211118041286.html