List of string constants and C++

Technical questions regarding the XTC tools and programming with XMOS.
Post Reply
User avatar
data
Active Member
Posts: 43
Joined: Wed Apr 06, 2011 8:02 pm

List of string constants and C++

Post by data »

Using restricted pointers, I can set up a list of string constants in XC and pass it around:

Code: Select all

#include <platform.h>
#include <stdio.h>
#include <xs1.h>
#include <xccompat.h>

void print_str_list(const char* strlist[])
{
    int i = 0;
    while (strlist[i]) {
        printf(strlist[i]);
        printf("\n");
        ++i;
    }
}

int main()
{
    const char* restrict strlist[] = {
        "fee", "fi", "foo", "bar", 0
    };

    print_str_list(strlist);

    return 0;
}
However, I can't find any way to pass an array like strlist to C/C++. Is such a thing possible?

NULLABLE_ARRAY_OF from xccompat seems not to be useful here, as it seems the string constant needs to be declared
const char[]
for use with C/C++.


User avatar
ers35
Active Member
Posts: 62
Joined: Mon Jun 10, 2013 2:14 pm
Contact:

Post by ers35 »

At first I tried this, but the program crashes because sizeof(&list[0]) is not the same in XC and C so the types don't match. I wonder if the compiler should produce a warning:

Code: Select all

// string-constant.xc
// xcc -target=XS1-L4A-64-TQ48-C4 string-constant.xc string-constant.c

#include <stdio.h>

extern "C" {
  void print_str_list(const char* list[], size_t size);
}

int main()
{
  const char* restrict list[] = {
    "fee", "fi", "foo", "bar",
  };
  print_str_list(list, sizeof(list) / sizeof(list[0]));
  return 0;
}

Code: Select all

// string-constant.c

#include <stdio.h>

void print_str_list(const char* list[], size_t size)
{
  for (int i = 0; i < size; i += 1)
  {
    printf("%s\n", list[i]);
  }
}
Unsafe works:

Code: Select all

// string-constant.xc
// xcc -target=XS1-L4A-64-TQ48-C4 string-constant.xc string-constant.c

#include <stdio.h>

extern "C" {
  void print_str_list(const char* list[], size_t size);
}

int main()
{
  unsafe
  {
    const char* unsafe list[] = {
      "fee", "fi", "foo", "bar",
    };
    print_str_list(list, sizeof(list) / sizeof(list[0]));
  }
  return 0;
}

Code: Select all

// string-constant.c

#include <stdio.h>

void print_str_list(const char* list[], size_t size)
{
  for (int i = 0; i < size; i += 1)
  {
    printf("%s\n", list[i]);
  }
}
Post Reply