Home > Software design >  error: expected expression before ']' token when passing an array as an arguement in C
error: expected expression before ']' token when passing an array as an arguement in C

Time:03-28

I am new to C. Was writing this so it takes the strings from the passed array and makes it a single sentence. But I got this error, I am not good with arrays in C. I can use some help from you guys. I did search an answer for this and couldn't find.

#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


char smash(char arr[20][20]) {
    char tmp[sizeof(arr)/sizeof(arr[0])];
    for (int i=0; i < sizeof(arr)/sizeof(arr[0]); i  ) {
        strcat(tmp, arr[i]);
        strcat(tmp, " ");

    }
    return tmp;
}

int main(){
    char list[][6] = {"hello", "world"};
    printf("%s", smash(list[]));
}

Error

error: expected expression before ']' token
printf("%s", smash(list[]));
                        ^

CodePudding user response:

There are quite a number of errors in this small piece of code.

First, to address the compiler error: list[] is not a valid expression. If you want to pass list to the function, leave the braces out:

printf("%s", smash(list));

This will then bring up another error. The function is expecting a char [20][20] as it's argument, but that's not what you're passing in. Since arrays as parameters are converted to a pointer, the argument type is actually char (*)[20] i.e. a pointer to an array of char of size 20. Note also that this conversion only occurs for the outermost array dimension, not all.

Since you're passing in a char [2][6] which gets converted to a char (*)[6] this is a type mismatch. So change the parameter to char arr[][6].

Then you're attempting to get the size of the array parameter inside of the function:

sizeof(arr)/sizeof(arr[0])

Since arrays cannot be directly passed to a function due to the conversion mentioned earlier, arr is actually a pointer and not an array, so you won't get the result you expect from this. You'll need to pass the number of array elements as a separate parameter.

Then you're calling strcat on tmp. This function will only work if the destination already has a null terminated string in it. Since tmp was not initialized or written to prior to the first call to strcat, you end up reading uninitialized bytes and potentially past the end of the array which will trigger undefined behavior.

This can be fixed by setting the first byte of the array to 0 before the loop to make it an empty string:

tmp[0] = 0;
for ...

Then there's the problem with the return type. The function is declared to return a char but you're giving a char * to the return statement, and at the point the function is called it is passed to printf where the %s format specifier is expecting a char * parameter.

So change the return type of the function from char to char *.

Finally, you're returning a pointer to a local variable in the function. This variable's lifetime ends when the function returns, so the returned pointer is invalid and using it will also trigger undefined behavior.

You'll need change tmp to a pointer and dynamically allocate memory for it using malloc. This also means you'll need to save the return value of the function in a separate variable which you can then pass to printf to print and then pass to free to free the memory.

After making all this changes, the resulting code should look like this:

char *smash(char arr[][6], int len) {
    // enough for len strings plus len spaces
    char *tmp = malloc(sizeof(arr[0]) * len   len   1); 
    tmp[0] = 0;
    for (int i=0; i < len; i  ) {
        strcat(tmp, arr[i]);
        strcat(tmp, " ");

    }
    return tmp;
}

int main(){
    char list[][6] = {"hello", "world"};
    char *result = smash(list, sizeof(list)/sizeof(list[0]));
    printf("%s", result);
    free(result);
    return 0;
}
  • Related