-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;
}
3
  • Edit your code and indent it properly, if you expect anyone to read this question.
    – Lundin
    Jan 25, 2017 at 10:07
  • it would be a lot easier if you would use the english language in your code.
    – Kami Kaze
    Jan 25, 2017 at 10:16
  • Sorry, you're right. I forgot to change language. Jan 25, 2017 at 12:33

2 Answers 2

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);
0
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

2
  • Thank you! Now I understand where's the error. Very kind. Jan 25, 2017 at 10:18
  • 1
    @FranzGoogle - Be mindful of what you understood. It's only now a correct answer. Jan 25, 2017 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.