在 C 中编辑 char 数组,输出被截断
Edit char array in C, output cut off
首先,我知道有很多关于这个话题的问题。但是我已经搜索了很多并测试了我发现的类似问题但没有成功的解决方案。
我想在调用 foo 时编辑一个字符数组,在调用 bar 时编辑另一个字符数组,但输出一直被切断(见下文)。
main.c
#include "struct.h"
static bool on = true;
static long val = 1;
static char first[15];
static char value[13];
static char second[15];
void foo(void)
{
strcpy(first, (char*)hello); // first = "Hello "
LongToStr(val, value); // value = "1"
Ltrim(value);
Rtrim(value);
strcat(first, value); // first = "Hello 1"
strcat(first, (char*)world); // first = "Hello 1 world"
set(0, first);
}
void bar(void)
{
on =! on;
strcpy(second, (char*)fooBar); // second = "fooBar "
if (on)
strcat(second, (char*)OnText); // second = "fooBar on"
else
strcat(second, (char*)OffText); // second = "fooBar off"
set(1, second);
}
int main()
{
init();
foo();
printf("%s", get(0));
foo();
printf("%s", get(0));
bar();
printf("%s", get(1));
bar();
printf("%s", get(1));
}
struct.c
#include "struct.h"
static myStruct a[2];
void init(void)
{
a[0].b = hello;
a[1].b = fooBar;
}
void set(short pos, char* new)
{
a[pos].b = new;
}
const char* get(short pos)
{
return a[pos].b;
}
struct.h
void init(void);
void set(short pos, char* new);
const char* get(short pos);
typedef struct {
const char *b;
} myStruct;
static const char hello[] = "Hello ";
static const char world[] = " world";
static const char fooBar[] = "fooBar ";
static const char OnText[] = "on";
static const char OffText[] = "off";
输出:
1
1
on
说真的,我在这里错过了什么?
通过不将 char 数组声明为常量解决了这个问题(但不确定为什么)。
static char hello[] = "Hello ";
static char world[] = " world";
static char fooBar[] = "fooBar ";
static char OnText[] = "on";
static char OffText[] = "off";
首先,我知道有很多关于这个话题的问题。但是我已经搜索了很多并测试了我发现的类似问题但没有成功的解决方案。
我想在调用 foo 时编辑一个字符数组,在调用 bar 时编辑另一个字符数组,但输出一直被切断(见下文)。
main.c
#include "struct.h"
static bool on = true;
static long val = 1;
static char first[15];
static char value[13];
static char second[15];
void foo(void)
{
strcpy(first, (char*)hello); // first = "Hello "
LongToStr(val, value); // value = "1"
Ltrim(value);
Rtrim(value);
strcat(first, value); // first = "Hello 1"
strcat(first, (char*)world); // first = "Hello 1 world"
set(0, first);
}
void bar(void)
{
on =! on;
strcpy(second, (char*)fooBar); // second = "fooBar "
if (on)
strcat(second, (char*)OnText); // second = "fooBar on"
else
strcat(second, (char*)OffText); // second = "fooBar off"
set(1, second);
}
int main()
{
init();
foo();
printf("%s", get(0));
foo();
printf("%s", get(0));
bar();
printf("%s", get(1));
bar();
printf("%s", get(1));
}
struct.c
#include "struct.h"
static myStruct a[2];
void init(void)
{
a[0].b = hello;
a[1].b = fooBar;
}
void set(short pos, char* new)
{
a[pos].b = new;
}
const char* get(short pos)
{
return a[pos].b;
}
struct.h
void init(void);
void set(short pos, char* new);
const char* get(short pos);
typedef struct {
const char *b;
} myStruct;
static const char hello[] = "Hello ";
static const char world[] = " world";
static const char fooBar[] = "fooBar ";
static const char OnText[] = "on";
static const char OffText[] = "off";
输出:
1
1
on
说真的,我在这里错过了什么?
通过不将 char 数组声明为常量解决了这个问题(但不确定为什么)。
static char hello[] = "Hello ";
static char world[] = " world";
static char fooBar[] = "fooBar ";
static char OnText[] = "on";
static char OffText[] = "off";