4

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!!

1
  • 2
    You're not passing a char [], you're passing a char *[] or char ** Commented May 14, 2016 at 9:23

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It should be void function1(const char *my_array[]) to match the declaration of the my_array variable in main.
@mch You are right, OP should add const as well. Thanks!
1

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");
}

Comments

1

Ok guys I've tried all your solutions and when I add "const" in the function it gives me an error "error in conflicting types".

Reading all of you I figured out that I was passing only a character in the function not a string of characters (the * was missing in the initialize of the function and the function).

I changed this:

void function1(char[]);

To this:

void function1(char*[]);

and this:

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

to this:

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

And it works like a charm!

Sorry if it was a bit of a noob question but i'm starting with C programming.

Thank you all for your answers I appreciate it!

1 Comment

:) Good to hear! Keep it up!
0

Assuming that you want a function which loops through the elements of an array and prints all its contents, here's what I suggest:

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


void view(char *array[]){
    int i;
    for (i = 0; i < sizeof(array)/sizeof(char); i++){ 
        //sizeof(array)/sizeof(char) = length of any array          
        printf("\nElement -> %s",array[i]);
    }
}


int main() {

    char *arr[] = {"String One", "String Two", "String Three"};

    view(arr); //Calling the function

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.