Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing strings from an array in a function in C

Tags:

arrays

c

string

I keep getting a crash in the case 4 of my switch in my main function and cant fix it.

I will explain the code a bit and hope you guys can help me:

Initializing the function

void function1(char[]);

Declaring the array of strings

const char *my_array[] = {
"Array of strings one",
"Array of strings two",
"Array of strings three"};

Looping through the array of strings in the main function (This works correctly, it prints the array of strings)

int i;
for (i=0; i < 3; i++) {
    printf("%s\n", my_array[i]);
}

The code in the switch function (still in the main function)

case 4:
            function1(my_array);
            break;

I've tested and all of the previous code works correctly, the problem is in here (outside the main function):

void function1(char my_array[]) {
for (i=0; i < 3; i++) {
    printf("%s\n", my_array[i]);
}
printf("\n");}

When I execute the case 4 of the switch, it crashes.

The 2 warning the log gives:

warning: passing argument 1 of 'function1' from incompatible pointer type

warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]

Sorry if the explanation its a bit unclear I tried as hard as I could for it to be easy to understand.

I really hope you guys can help me, thanks!!

like image 287
Maxy Avatar asked Dec 18 '22 17:12

Maxy


2 Answers

The first warning, namely

warning: passing argument 1 of 'function1' from incompatible pointer type

tells the story: you are passing an array of character pointers, but the function declaration says that it wants a simple character pointer.

The second warning tells you the same thing from the inside of the function:

warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat=]

The format wants a C string, but you are passing a single char*.

All you need to do to fix this is to add a missing asterisk to the declaration of function1:

void function1(const char *my_array[])
//             ^^^^^      ^

While you are at it, add const to match the declaration of my_array in main.

*Compiler says that you are passing an int, not char, because printf takes variable number of parameters, so the compiler applies certain transformations to function's arguments. One of such transformations is promotion of all chars to ints.

like image 170
Sergey Kalinichenko Avatar answered Jan 03 '23 05:01

Sergey Kalinichenko


Please have a closer look at the function deceleration

void function1(char my_array[])

You are passing in char array which is string (which obviously you do not want). The second problem is with this line,

printf("%s\n", my_array[i]);

when you say my_array[i], you are trying to access a character at 'i' position and using %s to print a character (which is a crash). To print a character you should write this

printf(%c\n", my_array[i]);

Now lets fix the code according to your requirement, you should have a function which takes the array of char pointer (OR string)

void function1(const char* my_array[])
{
    for (size_t i = 0; i < 3; i++)
    {
        printf("%s\n", my_array[i]);
    }
    printf("\n");
}

Please notice the use of 'const' (if you just have to read/print the data from your string array). Please consider sending the size of array too and use that size to iterate through your loop.

void function1(const char* my_array[], size_t n)
{
    for (size_t i = 0; i < n; i++)
    {
        printf("%s\n", my_array[i]);
    }
    printf("\n");
}
like image 30
A.B. Avatar answered Jan 03 '23 05:01

A.B.