当前位置:网站首页>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;
}边栏推荐
- Tomorrow is the PMP Exam (June 25). Have you understood all this?
- Still using simpledateformat for time formatting? Be careful of project collapse
- 六大行数据治理现状盘点:治理架构、数据标准与数据中台(2022.04)
- Stm32f030f4 reading infrared remote control data
- Classic interview questions and answers for embedded engineers
- HarmonyOS访问数据库实例(3)--用ORM Bee测下HarmonyOS到底有多牛
- Is there really something wrong with my behavior?
- Binary lookup array subscript
- 去商场逛街
- Andersen Global借助巴勒斯坦成员公司加强中东平台
猜你喜欢

抖音实战~实现App端视频上传与发布

Design and practice of vivo server monitoring architecture

抖音实战~项目关联UniCloud

7-6 laying oil well pipeline

Jetpack Compose 最新进展

抖音实战~发布短视频流程梳理

Actipro WPF Controls 2022.1.2

Annual salary of millions, 7 years of testing experience: stay at a fairly good track, accumulate slowly, wait for the wind to come

Go language pointer, value reference and pointer reference

国内有哪些好的智能家居品牌支持homekit?
随机推荐
Websocket long link pressure test
Installing IBM CPLEX academic edition | CONDA installing CPLEX
Helix distance of point
Huawei machine learning service speech recognition function enables applications to paint "sound" and color
Volcano成Spark默认batch调度器
Leetcode topic [array] -39- combined sum
R language dplyr package group_ By function and summarize_ The at function calculates the dataframe to calculate the number of counts and the mean value of different groups (summary data by category v
Modify stm32f030 clock source to internal crystal oscillator (HEI)
Pseudo original intelligent rewriting API Baidu - good collection
Why is it that the "Zhongtai" that was originally eaten by civil engineering is no longer fragrant?
7-5 maximal submatrix sum problem
From client to server
[basic knowledge] ~ half adder & full adder
国内有哪些好的智能家居品牌支持homekit?
257. detention of offenders
376. 機器任務
[JS] - [array, stack, queue, linked list basics] - Notes
R language uses the multinom function of NNET package to build an unordered multi classification logistic regression model, and uses the AIC function to compare the AIC values of the two models (simpl
Enterprise data leakage prevention solution sharing
无鸟用的SAP PA证书,刚入行的同行可以考一考