1

I'm a university student. and the teacher wants me to pass multiple strings to function. The answer output is correct but I wonder what the warning is why it shows and how to fix it in my case.

I try to figure it out but so many times I search. It just contains the other case and no one this like my case (using sting with pointer). So if you can additionally recommend me the website that has my case or something with passing 2d array to function I will appreciate it so much.

Here's the code

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


int hydroxide(char (*message)[100], int size);

int main()
{
    char message[6][100] = {"KOH", "H2O2", "NaCl", "NaOH", "C9H8O4", "MgOH"};
    int size;

    printf("Messages ending with OH are: ");

    for(int i = 0; i < 6; i++)
    {
        size = strlen(message[i]);

        if(hydroxide(message[i], size) == 1)
        {
            printf("%s ", message[i]);
        }
    }

    printf("\n");

    return 1;   
}

int hydroxide(char (*message)[100], int size)
{
   if(((*message)[size - 2] == 'O') && ((*message)[size - 1] == 'H'))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

When I try running it, it shows like this.

HW-4_5_3.c: In function 'main':
HW-4_5_3.c:19:22: warning: passing argument 1 of 'hydroxide' from incompatible pointer type [-Wincompatible-pointer-types]
         if(hydroxide(message[i], size) == 1)
                      ^~~~~~~
HW-4_5_3.c:6:5: note: expected 'char (*)[100]' but argument is of type 'char *'
 int hydroxide(char (*message)[100], int size);
     ^~~~~~~~~
Messages ending with OH are: KOH NaOH MgOH

The part that "Messages ending with OH are: KOH NaOH MgOH" is what I want. So, how can I fix it?

3 Answers 3

4

The first function parameter

int hydroxide(char (*message)[100], int size);

has the type char ( * )[100] but in the call of the function

if(hydroxide(message[i], size) == 1)

the corresponding argument has the type char *. That is the array message[i] having the type char[100] used as an argument expression is implicitly converted to pointer of the type char * to its first element

Declare and define the function the following way

int hydroxide( const char *message )
{
    size_t n = strlen( message );

    return n > 1 && message[n-2] == 'O' && message[n-1] == 'H';
}

and call it like

if ( hydroxide(message[i] ) )

Pay attention to that the function should have one parameter: a pointer to a string. The parameter should have the qualifier const because the function does not change the passed string. The second parameter is only confusing because it is assumed that the function deals with strings. So it itself should determine the length of the string and check whether the length is not less than 2.

There is no need to use two return statements within the if-else statement within the function. It is enough to write one return statement with an expression containing logical operators. In this case the function will return exactly either 1 or 0.

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

4 Comments

Thank you. And also I want to know if the compiler knows that char *message is a 2d array? or is it known because of char type? At first, I thought that it don't know that I declare the 2d parameter and I decided to use (*message)[100].
@LunaticCoder char *message declares a pointer to character. It a pointer to the first character of the string message[i] passed to the function as an argument expression.
So, it's no need to declare 2d array as char parameter. Did my understand is correct?
@LunaticCoder The function deals with strings. Strings are stored as one-dimensional arrays.
1

You are declaring a parameter that's an array of char pointers with a size of 100. Try changing it to a pointer

int hydroxide(char *message, int size);

You don't need to declare the size of null terminated strings as a parameter

Comments

0

If you have

int hydroxide(char (*message)[100], int size);

you need to supply a pointer to a char[100]:

if(hydroxide(&message[i], size) == 1)
//           ^

If you actually want the original call to work, you need to change hydroxide. Example:

int hydroxide(char *message, int size)
{
   if(size < 2) return 0; // added precaution

   if((message[size - 2] == 'O') && (message[size - 1] == 'H'))
    {
        return 1;
    }
    else
    {
        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.