Checking string within a function in C

0

Greetings! I am creating a C program where the user can change the state of a sensor entering the sensor name and 1 or 0 to change its status, 1 for on and 0 for off. However, when I try to check this condition out of the main, in a function is not working, I believe it is an error in the parameter passing if they can help me, I'll put the code below.

#include<stdio.h>
#include<string.h>
char y[2];
int sensor;
int s1;
char s[2];

void verificar(char y[2], int sensor);

void main ()
{
printf("Digite o sensor que deseja alterar o estado\n");
gets(s);
printf("Ligar ou desligar? (1 ou 0)\n");
scanf("%d", &s1);
void verificar(s, s1);
}

 void verificar(char y[2], int sensor)
{
if(strcmp(y,"s1")==0 && (s1 == 1))
{
    printf("S1 ligado");
}
}

When I try to run the program, verification does not occur.

    
asked by anonymous 24.02.2018 / 23:13

1 answer

0

Dude, your problem is that you're passing the vector by parameter wrong. The correct one when receiving a vector by parameter is to receive the address of it, that is, it needs a pointer.

The second point is that to call a function you do not put void.

With these changes your code worked:

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

char y[2];
int sensor;
int s1;
char s[2];

void verificar(char *y, int status){
    if(strcmp(y,"s1") == 0 && (status == 1)) {
        printf("S1 ligado");
    }
}

void main () {
    printf("Digite o sensor que deseja alterar o estado\n");
    gets(s);
    printf("Ligar ou desligar? (1 ou 0)\n");
    scanf("%d", &s1);
    verificar(s, s1);
}
    
24.02.2018 / 23:40