1

In CAPL, apparently you cannot pass a string as an input parameter for a user-defined function, according to the docs (see CAPL Introduction » Data Types for Function Parameters).

I'm working with file handling, both in read and write directions. I'd like to refactor my code to use a function that accepts the filename as input parameter.

Apart from the obvious workarounds, such as using a global variable, using a system/environment variable, I'm interested in the possibility of other alternatives.

How do you do it?


edit

CAPL does not provide the string type, much like C these are "just" arrays of characters.

In the help page I was mentioning, you can pass a single char as parameter function, but not a char[] array.

What, rightfully, mr. Spiller points out is that this piece of code works:

on start
{
    function("a string");
}

void function(char string[])
{
    write ("my string is %s", string);
}

and outputs:

CAPL / .NET my string is a string

However, that looks like an associative array to me.

For instance, this compiles as well:

void function(int number[], char string[])
{
    // do stuff
}

But understanding what is going on is suddenly more difficult, as this won't compile:

on start
{
    function(13, "a string");
}

void function(int number[], char string[])
{
    write ("my number is %d", number);
    write ("my string is %s", string);
}

Error: types of parameters do not match.

Finally:

variables
{
    int associativeArray[ float ];
}

on start
{
    associativeArray[1] = 3;
    function(associativeArray, "a string");
}

void function(int number[float], char string[])
{
    for (float aKey: number)
    {
        write ("my number is %d(%f)", number[aKey], aKey);
    }
}

CAPL / .NET my number is 3(1.000000)

works as intended, but then again, I'm unsure of what the caveats of using an associative array are in this scenario (I can't find out a way of iterate over the string with the same syntax, for instance), and how do you address them, if you do.

Daemon Painter
  • 3,208
  • 3
  • 29
  • 44
  • Just to make sure we are on the same page: why do you think that you cannot pass a string? Why would `void func(char param[])` not work? – MSpiller Feb 10 '20 at 09:20
  • Mostly due to the reference page I was mentioning. I'm not entirely sure about how you are making this work, to be honest. I'm editing my question to make it clearer. – Daemon Painter Feb 10 '20 at 13:24

1 Answers1

3

The help page you are mentioning also says further below something like

and matrices of the aforementioned datatypes (I can't remember the exact wording and don't have access to a CANoe right now).

char[], int[] and so on are not associative arrays but arrays. You may think of them as associative array but the key may only be a positive integer.

As you say CAPL and C handle strings in the same way, i.e. as arrays of characters. Therefore, coming back to your original question using a definition like

void func(char param[])

is the way to go when you want to pass a string (which is just an array of characters) to a function.


This:

on start
{
    function(13, "a string");
}

void function(int number[], char string[])
{
    write ("my number is %d", number);
    write ("my string is %s", string);
}

does not compile, because 13 is not an array of integers, but just a single integer. The compiler therefore says the the types do not match.

This will work:

on start
{
  int i[1];
  i[0] = 13;
  function(i, "a string");
}

The same way the following code will also NOT compile:

void function(char string[])
{
   ...
}

on start
{
  function('a');
}

'a' (note the single quotes) is just a single character and no array of characters and therefore the signature does not match.


You can get the individual characters of a string by simply using the [] operator (also same as in C).

char s[] = "abcde"; char c = s[2];

will store 'c' into s

Hope that explains it. Feel free to ask for further clarification. (I did type all this from top of my head, so please excuse any minor syntax errors)

MSpiller
  • 3,500
  • 2
  • 12
  • 24