First of all, I know that there are a lot of questions about this topic. But I have searched a lot and tested the solutions I found with similar issues but without success.
I want to edit a char array when foo is called and another when bar is called, but the output keeps getting cut off (see below).
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";
Output:
1
1
on
Seriously, what am I missing here?
Edit: Documentation of mikroC built-in functions: strcpy Ltrim Rtrim LongToStr strcat
Solved it by not declaring the char arrays as constants (but not sure why).
static char hello[] = "Hello ";
static char world[] = " world";
static char fooBar[] = "fooBar ";
static char OnText[] = "on";
static char OffText[] = "off";