当前位置:网站首页>The understanding of string in C.
The understanding of string in C.
2022-07-27 16:21:00 【bamboogz99】
For c language, the string is a kind of abstract data type, which can be defined as a char string.
char str[256];
However, what's is the length of a string. For example:
scanf("%s", str);
If use input:
abc<CR>
So, all of us know the length of str is 3, which can be calculated with lib function strlen.
int len = strlen(str);
Now, we have the following code segment:
char str[256];
scanf("%s", str);
char *a = (char*)malloc(strlen(str));
strcpy(a, str);
printf("%s\n", a);
free(a);Is the above code right? If no, find the problem and explain why.
If you run the above code in Visual studio 2010(I didn't test it in other IDE), it will prompt that "HEAP CORRUPTION DETECTED: after...". What's problem.
In fact, a default char will be added to mark the end of string in C language, it's NUL. Look at the following snapshot of a watch window.

You can see there is a '0' at the end of str, which is the ASCII of '\0'. Therefore, we can see that the actual size of a string in C should be strlen()+1. In above code, the function free() will release the memory space which has been allocated by malloc, i.e., strlen(str). To correct it, we should take the end sign of string into account and we get:
char str[256];
scanf("%s", str);
char *a = (char*)malloc(strlen(str)+1);
strcpy(a, str);
printf("%s\n", a);
free(a);At last, leave a question!
typedef struct node{
char* name;
struct node* next;
}Node;
//The following code is correct or not?
Node* a = (Node*)malloc(sizeof(struct node));
if(a)
{
char str[256];
scanf("%s", str);
a->name = (char*)malloc(strlen(str));
strcpy(a->name, str);
a->next = NULL;
}
free(a);边栏推荐
- js中的函数与DOM获取元素和事件属性的使用
- Full automatic breast pump chip dltap703sd
- Led with fan eye protection learning table lamp touch chip-dlt8s12a
- MySQL 05 stored procedure
- Nodejs 模板引擎ejs
- Imitation thread deduction
- MySQL 01 relational database design
- Leetcode brushes questions the next day
- JS to achieve smooth scrolling of pages or DOM elements
- 微信支付及支付回调
猜你喜欢
随机推荐
Uni app for wechat login (to be finished)
正则表达式的扩展
Low noise anion fan touch IC
JS to realize simple form verification and select all functions
Bathroom with demister vanity mirror touch chip-dlt8t10s
Overview of Baidu map technology, and application development of basic API and webapi
Interceptor interceptor
网红RGB镜子灯触摸芯片-DLT8S15B-杰力科创
Uni app form submit button send request
Day 3 of leetcode question brushing
LED带风扇护眼学习台灯触摸芯片-DLT8S12A
idea 2020.1社区版下载体验
TypeError: conv2d(): argument ‘padding‘ (position 5) must be tuple of ints, not str【报错】
地图找房的实例
连接查询和子查询
Hash、Set、List、Zset、BitMap、Scan
音乐律动七彩渐变灯芯片--DLT8S04A-杰力科创
[yuntu said] 249 mobile application security service - app's physical examination center, comprehensive testing, safe on the road!
Collection of software design suggestions of "high cohesion and low coupling"
Typeerror: conv2d(): argument 'padding' (position 5) must be multiple of ints, not STR [error]









