-4

Yesterday I asked for a help in this question, and now the situation is better, but not OK. I repost the correct code, but it returns another problem, despite the corrections. The second function reads all the array's values as 0. I think the problem is about the pointers, but I can't understand how to fix it.

Here's my code:

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

int leggiSequenza(char *nomeFile, int *seq) {

    FILE *in;
    int i;
    int dim;

    if((in = fopen(nomeFile, "r"))==NULL) {
        printf("Errore: impossibile leggere il fie in apertura.\n");
        return -1;
    }

    fscanf(in, "%d", &(dim));
    printf("Trovati %d valori.\n", dim);

    if(dim < 0) {
        printf("Errore: il numero di interi risulta negativo.\n");
        return -1;
    }

    seq = (int*) malloc(dim*sizeof(int));

    i=0;
    while(!feof(in) && i<(dim)) {
        fscanf(in, "%d", &seq[i]);
        i++;
    }

    for(i=0; i<(dim); i++) {
        printf("Il valore letto in posizione %d è: %d.\n", i+1, seq[i]);
    }


    fclose(in);
    free(seq);

    return dim;
    }




int numeroPassi(int *valori, int size) {

    int i;
    int somma;
    int passi[size];

    for(i=0; i<size; i++) {
        printf("valore in posizione %d = %d.\n", i+1, valori[i]);
    }

    somma=0;
    for(i=0; i<(size-1); i++) {
        somma = somma + abs(valori[i]);
    }

    printf("La somma del valore assoluto di tutti gli elementi è: %d.\n", somma);

    return 0;
}




int main(int argc, char* argv[]) {

    char nomeFile[200];
    int passi;

    printf("\n");

    printf("Inserire il nome del file:\n");
    scanf("%s", nomeFile);
    printf("\n");

    int * p = malloc(200*sizeof(int));
    int dim = leggiSequenza(nomeFile, p);
    printf("dimensione = %d\n", dim);
    printf("\n");

    passi = numeroPassi(p, dim);

    printf("\n");

    free(p);

    return 0;
}
Community
  • 1
  • 1
FranzGoogle
  • 451
  • 3
  • 5
  • 14

2 Answers2

2

You are not writing to the array you passed in to leggiSequenza. Instead you are allocating a new array that you write to and then free.

Remove the following lines from leggiSequenza to fix that problem:

seq = (int*) malloc(dim*sizeof(int));

and

free(seq);
Klas Lindbäck
  • 33,105
  • 5
  • 57
  • 82
1
int * p = malloc(200*sizeof(int));

Ok, here you allocated some memory. Now you pass that pointer p into first function:

int dim = leggiSequenza(nomeFile, p);

Inside this function you have a variable seq that is a pointer actually pointing to the same memory that p does.

Let's look further: seq = (int*) malloc(dim*sizeof(int)); Here you allocated new memory and afterwards do something with it.

Then you call free(seq) thus the memory allocated by second call to malloc is cleared (memory pointed to by p is still valid but remains unchanged)

Now you call passi = numeroPassi(p, dim);. Here memory pointed to by p is used as if there was no changes by first function

Yuriy Ivaskevych
  • 956
  • 8
  • 18