Hands with variable float and char

Asked

Viewed 54 times

1

I am unable to assign an address to a pointer variable when it comes to float or char variables, the visual studio brings me the error "C++ a value of type cannot be Assigned to an Entity of type". With the variable int the pointing works normally, in case my variable int x is ok, now the float y and char z of the problem as below.

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

int main()
{
    //Declaração das variaveis e inicialização.
    int x = 12; 
    float y = 22.20;
    char z = 'u';

    int *ponteiro_x, *ponteiro_y, *ponteiro_z; //declaração dos ponteiros

        ponteiro_x = &x; //ok
        ponteiro_y = &y; //erro
        ponteiro_z = &z; // erro


    system("pause");
    return 0;
}

Thanks for the help.

  • 2

    Yes, you have set onteiro_x, pointer_y and pointer_z as pointer to int, so only ponteiro_x = &x; is correct. State float*ponteiro_y; char *ponteiro_z;that will be correct.

1 answer

4

As colleague @anonimo commented, the error in your code is in declaring the types of pointers.

If Voce has a variable y of the float type, Voce needs to create a float pointer. The same thing applies to char z. In its code, Voce defined as int all pointers, but the right one is to define the pointers according to each type of variable that Voce has, according to the code below:

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

int main()
{
//Declaração das variaveis e inicialização.
int x = 12; 
float y = 22.20;
char z = 'u';

int *ponteiro_x; //declaração do ponteiro da variavel x
float *ponteiro_y; //declaração do ponteiro da variavel y
char *ponteiro_z; //declaração do ponteiro da variavel z

    ponteiro_x = &x; //ok
    ponteiro_y = &y; // irá funcionar.
    ponteiro_z = &z; // irá funcionar.


system("pause");
return 0;
}

Browser other questions tagged

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