C 中的 strcpy 无法正确复制以覆盖字符串
strcpy in C does not copy properly to overwrite a string
我正在尝试在 C 中实现中缀到后缀的转换程序。我编写了 (cleanExpression) 以下函数来删除给定中缀字符串表达式中不需要的 space。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#define EXPR_LIMIT 1000
bool isNumber(char c) {
return (c >= '0' && c <= '9');
};
bool cleanInfixExpression(char *expr, char **clean) {
*clean = malloc(sizeof(char) * strlen(expr) + 1);
strcpy(*clean, expr);
char *curr = *clean;
char *temp;
char *temp_dest;
// first count the number of terms (numbers and operators).
while (*curr != 0) {
// check if the current position is space.
if (*curr == ' ') {
printf("Current:%s\n", curr);
printf("Clean :%s\n\n", *clean);
// go through all space positions.
for(temp = curr; *temp == ' '; temp++) {};
// make its numbers on both side of the space.
if (curr != *clean && (isNumber(*(curr-1)) && (isNumber(*temp)))) {
printf("\"%s\" : Invalid expression!\n", expr);
free(*clean);
return (false);
}
// copy into the the original string.
strcpy(curr, temp);
}
curr++;
}
printf("Current:%s\n", curr);
printf("Clean :%s\n\n", *clean);
return (true);
};
void main(void) {
char expr[EXPR_LIMIT] = " 214 + 9523 - 235235";
printf("INFIX expression: %s\n", expr);
char *clean;
if (cleanInfixExpression(expr, &clean)) {
printf("%s\n", clean);
}
return;
}
当我运行这段代码时,下面是我得到的输出。
INFIX expression: 214 + 9523 - 235235
Current: 214 + 9523 - 235235
Clean : 214 + 9523 - 235235
Current: + 9523 - 235235
Clean :214 + 9523 - 235235
Current: 952 9523 - 235235
Clean :214+ 952 9523 - 235235
Current: 9523 - 235235
Clean :214+952 9523 - 235235
" 214 + 9523 - 235235" : Invalid expression!
但是,如果我将 strcpy(curr, temp) 更改为以下代码,并在其中手动复制,则会得到正确的输出。
temp_dest = curr;
while(*temp != 0) {
*(temp_dest++) = *(temp++);
}
*temp_dest = 0;
这是我手动复制字符串的输出,而不是使用 strcpy 函数。
INFIX expression: 214 + 9523 - 235235
Current: 214 + 9523 - 235235
Clean : 214 + 9523 - 235235
Current: + 9523 - 235235
Clean :214 + 9523 - 235235
Current: 9523 - 235235
Clean :214+ 9523 - 235235
Current: - 235235
Clean :214+9523 - 235235
Current: 235235
Clean :214+9523- 235235
Current:
Clean :214+9523-235235
214+9523-235235
我无法理解我在使用 strcpy 时犯了什么错误。谁能告诉我发生了什么事?
您不需要为 cleanInfixExpression
使用 malloc
只是为了删除空格:
这样做
void cleanInfixExpression(char *expr)
{
int r = 0;
int w = 0;
for (; expr[r]; r++) {
if (expr[r] != ' ') {
expr[w] = expr[r];
++w;
}
}
expr[w] = 0;
}
它将从 expr
中删除不需要的空格
来自标准:
7.21.2.3 The strcpy function
Description
2 The strcpy function copies the string pointed to by s2 (including the terminating null
character) into the array pointed to by s1. If copying takes place between objects that
overlap, the behavior is undefined.
所以你不能像你那样strcpy
重叠字符串。您需要编写自己的 strcpy
实现或使用 memmove
.
我正在尝试在 C 中实现中缀到后缀的转换程序。我编写了 (cleanExpression) 以下函数来删除给定中缀字符串表达式中不需要的 space。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#define EXPR_LIMIT 1000
bool isNumber(char c) {
return (c >= '0' && c <= '9');
};
bool cleanInfixExpression(char *expr, char **clean) {
*clean = malloc(sizeof(char) * strlen(expr) + 1);
strcpy(*clean, expr);
char *curr = *clean;
char *temp;
char *temp_dest;
// first count the number of terms (numbers and operators).
while (*curr != 0) {
// check if the current position is space.
if (*curr == ' ') {
printf("Current:%s\n", curr);
printf("Clean :%s\n\n", *clean);
// go through all space positions.
for(temp = curr; *temp == ' '; temp++) {};
// make its numbers on both side of the space.
if (curr != *clean && (isNumber(*(curr-1)) && (isNumber(*temp)))) {
printf("\"%s\" : Invalid expression!\n", expr);
free(*clean);
return (false);
}
// copy into the the original string.
strcpy(curr, temp);
}
curr++;
}
printf("Current:%s\n", curr);
printf("Clean :%s\n\n", *clean);
return (true);
};
void main(void) {
char expr[EXPR_LIMIT] = " 214 + 9523 - 235235";
printf("INFIX expression: %s\n", expr);
char *clean;
if (cleanInfixExpression(expr, &clean)) {
printf("%s\n", clean);
}
return;
}
当我运行这段代码时,下面是我得到的输出。
INFIX expression: 214 + 9523 - 235235
Current: 214 + 9523 - 235235
Clean : 214 + 9523 - 235235
Current: + 9523 - 235235
Clean :214 + 9523 - 235235
Current: 952 9523 - 235235
Clean :214+ 952 9523 - 235235
Current: 9523 - 235235
Clean :214+952 9523 - 235235
" 214 + 9523 - 235235" : Invalid expression!
但是,如果我将 strcpy(curr, temp) 更改为以下代码,并在其中手动复制,则会得到正确的输出。
temp_dest = curr;
while(*temp != 0) {
*(temp_dest++) = *(temp++);
}
*temp_dest = 0;
这是我手动复制字符串的输出,而不是使用 strcpy 函数。
INFIX expression: 214 + 9523 - 235235
Current: 214 + 9523 - 235235
Clean : 214 + 9523 - 235235
Current: + 9523 - 235235
Clean :214 + 9523 - 235235
Current: 9523 - 235235
Clean :214+ 9523 - 235235
Current: - 235235
Clean :214+9523 - 235235
Current: 235235
Clean :214+9523- 235235
Current:
Clean :214+9523-235235
214+9523-235235
我无法理解我在使用 strcpy 时犯了什么错误。谁能告诉我发生了什么事?
您不需要为 cleanInfixExpression
使用 malloc
只是为了删除空格:
这样做
void cleanInfixExpression(char *expr)
{
int r = 0;
int w = 0;
for (; expr[r]; r++) {
if (expr[r] != ' ') {
expr[w] = expr[r];
++w;
}
}
expr[w] = 0;
}
它将从 expr
中删除不需要的空格来自标准:
7.21.2.3 The strcpy function
Description
2 The strcpy function copies the string pointed to by s2 (including the terminating null character) into the array pointed to by s1. If copying takes place between objects that overlap, the behavior is undefined.
所以你不能像你那样strcpy
重叠字符串。您需要编写自己的 strcpy
实现或使用 memmove
.