void main()
{
    int number;
    totalCallsPtr pcall=NULL;       
    getData(&pcall,&number);    
}

void getData(totalCallsPtr* pcall,int* number)//get data from the text file
    { 
        int i,n;

    FILE *f1;
    f1=fopen(INPUT, "r");   
    if (f1==NULL)
        printf("File not open!\n");
    else
    {
        fscanf(f1,"%d",&n);

        *pcall=((totalCallsPtr)malloc(n*sizeof(CALL)));     
    }

    if((*pcall=(totalCallsPtr)malloc(n*sizeof(CALL)))==NULL)
        printf("not enough memory to allocate");

                for(i=0;i<n;i++)
                {

                fscanf(f1,"%d ",&pcall[i]->day);
                fscanf(f1,"%d:%d:%d ",&pcall[i]->startCall.hours,&pcall[i]->startCall.minutes,&pcall[i]->startCall.seconds);
                fscanf(f1,"%d:%d:%d ",&pcall[i]->endCall.hours,&pcall[i]->endCall.minutes,&pcall[i]->endCall.seconds);
                fscanf(f1,"%c",&pcall[i]->type);

                }
                free(*pcall);
                *number=n;
    }

why doesnt it work for me?

Firstly, saying things like 'why doesnt it work for me' does not help us understanding your problem, please specify what exactly is the problem..

Firstly, saying things like 'why doesnt it work for me' does not help us understanding your problem, please specify what exactly is the problem..

when i complile the prgoram quits with RUN TIME ERROR

Ok, though its not clearly mentioned that what exactly is 'totalCallsPtr' but it can be assumed that its a type defined as pointer to some structure..

My first point of concern:

else
{
fscanf(f1,"%d",&n);

*pcall=((totalCallsPtr)malloc(n*sizeof(CALL))); // 1
}

if((*pcall=(totalCallsPtr)malloc(n*sizeof(CALL)))==NULL) // 2
printf("not enough memory to allocate");

You have allocated a chunk of memory on heap in '1' and gave the address to '*pcall' again you allocated chunk of memory on heap in '2' and stored the address in the same '*pcall' which was previously pointing to a memory allocated on heap in '1'....so, in a nutshell u have lost the control over chunk of memory allocated in '1'

My second point of concern:

for(i=0;i<n;i++)
{

fscanf(f1,"%d ",&pcall[i]->day);
fscanf(f1,"%d:%d:%d ",&pcall[i]->startCall.hours,&pcall[i]->startCall.minutes,&pcall[i]->startCall.seconds);
fscanf(f1,"%d:%d:%d ",&pcall[i]->endCall.hours,&pcall[i]->endCall.minutes,&pcall[i]->endCall.seconds);
fscanf(f1,"%c",&pcall[i]->type);

}

When the value of i becomes '1' then 'pcall' means *(pcall+i) ie *(pcall+1) which means a jump to next structure in memory but pcall points to only single structure and the memory to that structure is allocated on heap

Ok, though its not clearly mentioned that what exactly is 'totalCallsPtr' but it can be assumed that its a type defined as pointer to some structure..

My first point of concern:

else
{
fscanf(f1,"%d",&n);

*pcall=((totalCallsPtr)malloc(n*sizeof(CALL))); // 1
}

if((*pcall=(totalCallsPtr)malloc(n*sizeof(CALL)))==NULL) // 2
printf("not enough memory to allocate");

You have allocated a chunk of memory on heap in '1' and gave the address to '*pcall' again you allocated chunk of memory on heap in '2' and stored the address in the same '*pcall' which was previously pointing to a memory allocated on heap in '1'....so, in a nutshell u have lost the control over chunk of memory allocated in '1'

My second point of concern:

for(i=0;i<n;i++)
{

fscanf(f1,"%d ",&pcall[i]->day);
fscanf(f1,"%d:%d:%d ",&pcall[i]->startCall.hours,&pcall[i]->startCall.minutes,&pcall[i]->startCall.seconds);
fscanf(f1,"%d:%d:%d ",&pcall[i]->endCall.hours,&pcall[i]->endCall.minutes,&pcall[i]->endCall.seconds);
fscanf(f1,"%c",&pcall[i]->type);

}

When the value of i becomes '1' then 'pcall' means *(pcall+i) ie *(pcall+1) which means a jump to next structure in memory but pcall points to only single structure and the memory to that structure is allocated on heap

#include <stdio.h>
#include <stdlib.h>
#define INPUT "tel.txt"


typedef struct
{
int hours;
int minutes;
int seconds;
} START_TIME ;

typedef struct
{
int hours;
int minutes;
int seconds;
} END_TIME ;

typedef struct
{
int day ;
START_TIME startCall;
END_TIME endCall;
char type;
} CALL ;

typedef CALL* totalCallsPtr;

sorry ..this is the struct.

um im a little bit confused? what i should do then so i could keep the malloc in the function?

With reference to my earlier post

1) For 'My first point of concern' just replace the code snippet

else
{
fscanf(f1,"%d",&n);

*pcall=((totalCallsPtr)malloc(n*sizeof(CALL)));
}

if((*pcall=(totalCallsPtr)malloc(n*sizeof(CALL)))==NULL)
printf("not enough memory to allocate");

with

else
{
fscanf(f1,"%d",&n);

if((*pcall=(totalCallsPtr)malloc(n*sizeof(CALL)))==NULL)
printf("not enough memory to allocate");
}

2) For 'My second point of concern', i assume that the file from which you are reading the values contains only a single line so change the following code snippet:

for(i=0;i<n;i++)
{

fscanf(f1,"%d ",&pcall[i]->day);
fscanf(f1,"%d:%d:%d ",&pcall[i]->startCall.hours,&pcall[i]->startCall.minutes,&pcall[i]->startCall.seconds);
fscanf(f1,"%d:%d:%d ",&pcall[i]->endCall.hours,&pcall[i]->endCall.minutes,&pcall[i]->endCall.seconds);
fscanf(f1,"%c",&pcall[i]->type);

}

with

fscanf(f1,"%d ",&pcall->day);
fscanf(f1,"%d:%d:%d ",&pcall->startCall.hours,&pcall->startCall.minutes,&pcall->startCall.seconds);
fscanf(f1,"%d:%d:%d ",&pcall->endCall.hours,&pcall->endCall.minutes,&pcall->endCall.seconds);
fscanf(f1,"%c",&pcall->type);

Hope it helps......

it containts 8 lines
or unknown size of lines thats why i used malloc
ummm so what u suggest me to do?

Major violations of the CODE TAG rule, that's the problem here.

malloc(n*sizeof(CALL))

Sorry i overlooked 'n' in the above line,So, you actually are allocating size to 'n' structures of CALL type. Now, I think you need to post the text file from which you are reading...I hope that is not difficult for you..

6
14 18:08:12 18:13:55 g
12 10:00:09 10:03:02 y
16 16:42:30 16:45:29 u
1 10:01:15 13:23:03 u
15 04:03:52 09:11:03 y
14 19:15:44 22:36:54 g

hey
i hope yuo didnt forget me :-)
any solution?

malloc(n*sizeof(CALL))

Sorry i overlooked 'n' in the above line,So, you actually are allocating size to 'n' structures of CALL type. Now, I think you need to post the text file from which you are reading...I hope that is not difficult for you..

hey
i hope you didnt forget me:)
any solution?

I wasn't in your original chain...but I'll try to help.

(I ended up compiling and running your code in the debugger)

Your problem is that you have a pointer to a pointer and you're indexing off the wrong pointer.

I changed your code: fscanf(f1,"%d ",&pcall[i]->day); to this code: fscanf(f1,"%d ",&(*pcall)[i].day); (with all of the other lines in that section) and the code no-longer faults.

I also commented out the free(*pcall); as you are returning the array to the caller. Either don't pass pcall to getData and then you can free it, or don't free the data you're returning to the caller.

Sorry.i was out of town....hope you got the answer...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.