First use of headers

I using first time the HEADERS in c so I'm not understanding it well.

This is main.c is code!!!
1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
#include <stdlib.h>
#include "kibe.h"

int main()
{
    int a[5],n,i;
    beolvas(a,n,"be.txt");
    kiir(a,n);
    return 0;
}


This is kibe.h code!!!
1
2
3
4
5
6
#ifndef KIBE_H_INCLUDED
#define KIBE_H_INCLUDED
void beolvas(int*,int ,const char *);
void kiir(int*,int);

#endif // KIBE_H_INCLUDED 


And this is kibe.c code!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>

void beolvas(int *a,int n,const char * file)
{
    int i;
    FILE * fin;
    fin = fopen("be.txt", "rt");
    fscanf(fin,"%i",&n);
    a = (int*)malloc(n*sizeof(int));
    for(i = 0; i < n; ++i){
        fscanf(fin,"%i",&a[i]);
    }
    free(a);
}
void kiir(int *a,int n)
{
    int i;
    for(i = 0; i < n; ++i){
        printf("%i ",a[i]);
    }
}


The problem is that I get memory garbage everytime and the file contains five numbers which must be read and write to monitor.If I write the void kiir is code to void beolvas function it works well.
Thanks,redbull1996
Last edited on
On line 8, you pass n to beolvas(), but n has not been initialized to any value yet, so you pass garbage.
Tha t is one thing what I forgot but I still get memory garbage.For example in the file I have the number 12 then I get a long number xD I really don't know the problem.
Topic archived. No new replies allowed.