Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C Language: I try passing 2d string to function and need to use pointer, but it shows the warning

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading