0

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

bac
  • 93
  • 1
  • 13
  • 2
    Note that using the header `"string.h"` is likely to cause confusion with the standard header `` — do not create your own header with the same name as a standard header. Learn and avoid creating standard header names. See [List of standard headers in C and C++](https://stackoverflow.com/q/2027991/15168). – Jonathan Leffler Jun 24 '21 at 13:55
  • 2
    `static myStruct a[1];` is an array of size 1. `a[1].b = fooBar;` is undefined behavior. All other questions are moot. – William Pursell Jun 24 '21 at 13:56
  • You could have it in one source file for us. What magic functions Ltrim Ltrim, LongToStr do? – 0___________ Jun 24 '21 at 13:56
  • 1
    `static myStruct a[1];` => `static myStruct a[2];` you access **two** elements not one!! – 0___________ Jun 24 '21 at 13:57
  • And, never use `gets`. Even if merely as a word in the title of the question! :) – William Pursell Jun 24 '21 at 13:59
  • 1
    The casts in `strcpy(first, (char*)hello);` and `strcpy(first, (char*)world);` are undesirable. They're unnecessary too — `strcpy()` takes a `const char * restrict` as the second argument, so the cast isn't needed. And it makes the code more verbose. Casts are useful in the correct places — but you should avoid using them as much as possible. – Jonathan Leffler Jun 24 '21 at 14:08
  • Does this actually compile? I see no declaration of `a` that is reachable from `main`. – HAL9000 Jun 24 '21 at 20:48
  • @jonathan sorry, updated the question. Copied some parts of the code without thinking it through. Also added links to documentation of the built-in functions. – bac Jun 28 '21 at 05:26
  • Please post the **exact** output you are getting, and the **exact** output you are expecting to get. Not your interpretations, just the characters that appear on the standard output. – n. m. could be an AI Jun 28 '21 at 05:43
  • Have you read the documentation you link? LongToStr: *Requires: Destination string should be at least 12 characters in length*. – n. m. could be an AI Jun 28 '21 at 05:47
  • @n.1.8e9-where's-my-sharem. the comments show the expected output, "**Output:**" at the bottom of my question show the actual output. Yes I have read it, but missed a 1 in the code I posted here, fixed it now. – bac Jun 28 '21 at 06:09

1 Answers1

0

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";
bac
  • 93
  • 1
  • 13