Error invalid Conversion from 'int' to 'int*' using -fpermissive

Asked

Viewed 1,846 times

0

I’m following a series of video-lessons in C (https://youtu.be/zZlIy3hp0c0?t=10m29s), but I found a problem.

First I ran this code:

#include <stdio.h>
int main(void)
{
    int x = 10; 
    int *resultado;
    resultado = &x;

    printf("Valor &x = %d", resultado);

    return 0;
}

And then I discovered that the result value (memory address) is 2358940.

So I tried to do it the same way the video lessons did:

#include <stdio.h>
int main(void)
{
    int x = 10;
    int *resultado;
    resultado = 2358940;

    printf("Valor x = %d", *resultado);

    return 0;
}

And I keep finding the same mistake, and doing exactly what the video lesson did. I don’t understand.

2 answers

4

The first code only compiles with options to pass possible problems. This is a warning. So far so good.

The second code doesn’t work because you’ll be picking up dirt. The last thing you should do is take an arbitrary value, play in a variable and try to access it as if it were a valid memory address. It is lottery. Programming should not be lottery. This should only be taught to say not to do.

His code worked in the conditions he was using. It will not work in any other condition. The video should alert this, and if it didn’t, it shows how bad it is. I tried and every execution gives a different address, there’s no trusting it, forget this nonsense. The only way to use a fixed address is something that’s documented that will always be there, which is rare these days.

By the way, the style of code presented is also bad. I wouldn’t worry too much if I was teaching people who have a good sense of programming and how languages work. But I realize that the goal is to teach laypeople. Then it creates bad addictions, since laypeople usually use these "lessons" as cake recipe.

The best recommendation I can give you is to stop watching these video lessons. And not only these, almost all are very weak, superficial and most of the time incorrect or at least misleading (in English has some better).

Look for better fonts to learn how programming really works. If you have any specific questions, put it here. Even when someone talks nonsense, someone else appears to show that they are wrong, so you do not run a lot of risk of learning wrong (this was better, today some things go wrong).

  • It’s your decision, I told you the address changes every time.

1


Every time you run your program, the memory address where the variable x will be stored is different. Therefore, if you run the program once, copy the address, and try to run the program again, that address will no longer be valid. If you, while running the same program, enter the value, this operation will work.

#include <stdio.h>
int main(void)
{
    int x = 10; 
    int *resultado;
    char temp[100];
    int enderecoLido;

    resultado = &x;

    printf("Valor &x = %d\n", resultado);

    printf("Agora digite o valor do endereço de memoria: ");
    gets(temp);
    enderecoLido = atoi(temp);
    printf("Valor digitado: %d\n", enderecoLido);

    resultado = enderecoLido;
    printf("Valor x = %d", *resultado);

    return 0;
}

But as @Maniero said in her great response, this operation (entering with an arbitrary memory address and trying to interpret it) is not recommended.

Browser other questions tagged

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