Function with reference passage

3

I'm trying to make a simple function that counts diamonds, each diamond is given by a pair of '<' and '>'. But I'm trying to do pointers, but it's not giving me the correct result. I followed the "Simple didactics for pointers" , and according to it I think I'm doing it right. Where is my mistake? Instead of displaying the result, the number of diamonds, it always prints the same memory address.

Here are the relevant functions:

int main(int argc, char** argv) {
    int casos; // número de casos de teste
    int n;
    int *diamantes; // aponta para número de diamantes
    char vetor[1000]; //variável para armazenar a entrada

    scanf("%d", &casos);

    for (n = 0; n < casos; n++) {
        limparBuffer();
        ler(vetor);
        contador(vetor, diamantes);
        printf("%d\n", diamantes);
    }
    return (EXIT_SUCCESS);
}

int contador(char vetor[], int *d) {
    int i, contEsq = 0, contDir = 0;
    int n = 1;
    for (i = 0; vetor[i] != '
int main(int argc, char** argv) {
    int casos; // número de casos de teste
    int n;
    int *diamantes; // aponta para número de diamantes
    char vetor[1000]; //variável para armazenar a entrada

    scanf("%d", &casos);

    for (n = 0; n < casos; n++) {
        limparBuffer();
        ler(vetor);
        contador(vetor, diamantes);
        printf("%d\n", diamantes);
    }
    return (EXIT_SUCCESS);
}

int contador(char vetor[], int *d) {
    int i, contEsq = 0, contDir = 0;
    int n = 1;
    for (i = 0; vetor[i] != '%pre%'; i++) {
        if (vetor[i] == '<')
            contEsq++;
        else if (vetor[i] == '>')
            contDir++;
    }


    if (contEsq > contDir)
        d = &contDir;
    else
        d = &contEsq;

}
'; i++) { if (vetor[i] == '<') contEsq++; else if (vetor[i] == '>') contDir++; } if (contEsq > contDir) d = &contDir; else d = &contEsq; }
    
asked by anonymous 02.01.2015 / 04:51

3 answers

5

Mastering content is just never doing something you do not need. Doing where you do not need to learn is wrong.

And it really does not make sense to do what you're doing. I can answer you the following: do not do this, you have a basic error understanding of variable scope. You are pointing to one of the two variables created in contador and then out of this function you will try to access that address when it should not be available anymore.

So what is the solution to this? It is very simple, return the value as any plausible code does. Trying to do something wrong will give the wrong result.

And worse, there is another problem. Good. Because if it did not, it would work by coincidence and you would think it was doing right.

When you do d = &contDir is saying "get the address of variable contDir and play in d ". It will then be accessed out of function by diamantes and printed it out. In other words, it is sending the address. And this address should not be accessed in any way, because it may not have what you expect.

So a possible solution is to get the operator from d = contDir . What is technically wrong and some compilers may signal problem.

Another way is to access diamantes by derreferencing: printf("%d\n", diamantes); . But you will continue to make a mistake in doing this because of the scope.

You probably made this mistake because you're wanting to use something at any cost. Things do not work that way.

There is still the solution: *d = contDir; . The most correct but totally unnecessary.

Take this parameter d and make a return that you should do even though you declared the type of this function as int .

Do not define a solution and look for a problem where it fits. This teaches no one. If you insist that you are learning something by doing this I will not tell you what this is called because some people would consider it aggressive. I'll just say it's stubborn.

I do not know if this is the only problem because the current code is not compileable, parts are missing.

    
02.01.2015 / 05:59
3

First, the solution to your problem:

1a) Change the int *diamantes statement to int diamantes

1b) Or, do int *diamantes = (int*) malloc(sizeof(int))

2) If you made 1a, change contador(vetor, diamantes) to contador(vetor, &diamantes) . If you made 1b, change printf("%d\n", diamantes) to printf("%d\n", *diamantes)

3) Change d = &contDir and d = &contEsq by *d = contDir and *d = contDir

4) If you made 1b, then before return (EXIT_SUCCESS) make free(diamantes)

Now, the explanation:

There is no pass-by-reference in C. Only, and only pass by value . It turns out that when you pass a pointer, the value passed is a memory address. With it, you can access the contents of this memory address using the * operator and change it.

What you were doing when writing d = &contDir was changing the value and not the content . So when the function returned, nothing had changed and your printf was printing an arbitrary address value because you had not initialized the pointer.

    
29.01.2015 / 00:49
0

Change the code to:

if (contEsq > contDir)
    d* = contDir;
else
    d* = contEsq;

And tell me if it worked.

    
02.01.2015 / 12:43