Gianmarco Moresi
Gianmarco Moresi

Reputation: 3

Application crash with pointer, header file and struct array

I've created a project in C with 3 files, main.c where I write a main:

main.c

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

typedef struct{
    char nome[29];
    char cognome[28];
    int et;
}s_persona;

int main(){
    s_persona personaX[5];

    caricamento(&personaX[5]);

    int i;

    for(i=0;i<=5;i++){
        printf("Nome: %s\t Cognome: %s\t Eta': %d\n", personaX[i].nome, personaX[i].cognome, personaX[i].et);
    }

    system("pause");
}

then a header file with a prototype (struct.h):

#ifndef STRUCT_H_
#define STRUCT_H_

void caricamento(s_pers perso[5])

#endif /* STRUCT_H_ */

and another source file with a function (struct.c):

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

typedef struct{
    char nome[29];
    char cognome[28];
    int et;
}s_pers;

void caricamento(s_pers* perso[5]){
    int k;

    for(k=0;k<=5;k++){
        printf("Inserisci nome dello studente: ");
        scanf("%s", perso[k]->nome);

        printf("Inserisci cognome dello studente: ");
        scanf("%s", perso[k]->cognome);

        printf("Inserisci l'eta' dello studente: ");
        scanf("%d", &perso[k]->et);
    }
}

Ok, there are all file that I used. Eclipse build project, without errors, but, whene I insert first string, the application stops working and crash. I try create another application be like this, but without use a struct array, and it's working perfectly... How I resolve this?

Thanks!!

Upvotes: 0

Views: 75

Answers (1)

rmm19433
rmm19433

Reputation: 280

Your header is not used at all. It would be best to define the structure in the header and include the header in your source files (as mentioned in the comments above).

#ifndef STRUCT_H_
#define STRUCT_H_

typedef struct{
    char nome[29];
    char cognome[28];
    int et;
} s_pers;

void caricamento(s_pers *perso)

#endif /* STRUCT_H_ */

Then in your main.c include the header and as also mentioned in the comments pass personaX. Otherwise you would pass the address of the sixth element, that does not even exist! Since arrays start at 0 you may only iterate until k < 5.

#include <stdlib.h>
#include <stdio.h>
#include <struct.h>    /* include your header! */

/* 
  Structure already defined in header, 
  no need to define it here! 
 */

int main(){
    s_pers personaX[5];

    caricamento(personaX);

    int i;
    for(i=0;i<5;i++){
        printf("Nome: %s\t Cognome: %s\t Eta': %d\n", 
            personaX[i].nome, personaX[i].cognome, personaX[i].et);
    }

    system("pause");
}

In your struct.c make sure to include your header and make sure your function matches the function declaration in your header file! Again make sure to stop at 5, so use k < 5.

#include <stdlib.h>
#include <stdio.h>
#include <struct.h>    /* include your header! */

void caricamento(s_pers* perso){
    int k;

    for(k=0;k<5;k++){
        printf("Inserisci nome dello studente: ");
        scanf("%s", perso[k].nome);

        printf("Inserisci cognome dello studente: ");
        scanf("%s", perso[k].cognome);

        printf("Inserisci l'eta' dello studente: ");
        scanf("%d", &perso[k].et);
    }
}

Upvotes: 0

Related Questions