Pointers in C language

Asked

Viewed 129 times

-1

I created a simple code with the function void where there is a passage by reference. However, it is giving the following error when compiling:

Lala. c: In Function 'main':

Lala. c:19:12: Warning: Passing argument 2 of 'test' from incompatible Pointer type [-Wincompatible-Pointer-types]

test (i, &a);

^ Lala. c:4:6: note: expected 'char **' but argument is of type 'char *'

void test (float i, char **a)

^~~~~

Code:

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

void teste (float i, char **a)    
{
    if (i<=7)
        *a = "Menor ou igual a sete";
    else    
        *a = "Maior que sete";
}

int main() {
    float i;
    char a;

    printf("Digite o número ");
    scanf("%f", &i);
    teste (i, &a);

    printf("%s\n", a);
    system ("pause");
    return 0;
}
  • 3

    You should get a char *a, instead of char **a. just need to change that. If you are going to receive a char through pointer, then you have to define that way

1 answer

-1

As quoted in the comments, the function teste() is wrong; use like this:

void teste (float i, char *a)
{
    if (i<=7)
        a = "Menor ou igual a sete";
    else
        a = "Maior que sete";
}

Do a search on the subject (parameters c), has a lot of useful material like this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.