6
Could someone explain to me the reason when I give free in the pontaux Windows terminal stops responding? And why when I allocate pontmaior my result comes as memory junk?
The function returns a pointer to the highest value of a vector.
Code:
#include <stdio.h>
#include <stdlib.h>
int* retornamaior (int *vetor, int tamanho) {
    int i, maior = vetor[0];
    for (i=0; i<tamanho; i++) {
        if (vetor[i]>=maior)
            maior = vetor[i];   
        }
    int *pontmaior = &maior;
    return pontmaior;
}
int main (int argc, char *argv[]) {
    int *vetor, tamanho, i;
    printf ("Digite o tamanho da sequência: ");
    scanf ("%d", &tamanho);
    vetor = (int*)malloc(tamanho*sizeof(int));
    printf ("Digite a sequência: ");
    for (i=0; i<tamanho; i++)
        scanf ("%d", &vetor[i]);
    int *pontaux = retornamaior (vetor,tamanho);
    printf ("Maior: %d\n", *pontaux);
    free (vetor);
    return 0;   
}
Kay, I don’t want to be a fanboy, but C++ does all this in ~12 lines...
– user2692
hehehe, but I’m learning C for now :D
– Kay
Yeah, kid, I started just like that :).
– user2692