problem with dynamic memory using malloc

hi guys actually i was wondering if u could help me out with this code as soon as possible

#include<stdio.h>
#include<sys/shm.h>
#include<stdlib.h>

#define MAX 20
#define BUFFER 1024

struct Process_Data
{
char Process_Name[MAX];
char * Addr;
int Sem_ID;
int Prc_ID;
int Shm_ID;
int BUFFER_SIZE;
};

struct Data_Mesg
{
int No_Of_Processes;
struct Process_Data *Mesg;
};

struct Data_Mesg Data=(struct Data_Mesg*) malloc(sizeof(struct Data_Mesg));

void Shm_Init(void)
{
struct Process_Data *P_Data,*Buf_Data;

int fd_N,fd_PES;
int n;

int shmid,key=1234;
int i=0,k=0;

char ch;
char *shm;

if((fd_N=open("/nparams/pr.txt",0666))==-1)
perror("N file open");

ch=getc(fd_N);
n=atoi(ch);

if((shmid=shmget(key,BUFFER,IPC_CREAT|0666))<0)
perror("Shared Memory get ");

shm=shmat(shmid,(char *) 0 ,0);

if((fd_PES=open("/nparams/PS.txt",0666))==-1)
perror("PES File open");

P_Data=(struct Process_Data *) malloc(n*sizeof(struct Process_Data));
Buf_Data=P_Data;

while(ch=(getc(fd_PES))!=EOF)
{
i=0;k=0;

while(ch!='\n')
{
Buf_Data->Process_Name[i++]=ch;
ch=getc(fd_PES);
}

Buf_Data->Addr=shm;
Buf_Data->Prc_ID=k++;
Buf_Data->BUFFER_SIZE=BUFFER;
Buf_Data->Shm_ID=shmid;
Buf_Data++;
}

Data.No_Of_Processes=n;
Data.Mesg=P_Data;

}

int main()
{
int i=0;
Shm_Init();
printf("\nThe no. of processes are %d\n",Data.No_Of_Processes);
printf("\nProcesses Name ProcessId \n");
while(i<Data.No_Of_Processes)
printf("%s %d\n",Data.Mesg->Process_Name,Data.Mesg->Prc_ID);
}

there are a lot of errors like
incompatible type conversion, illegal initialization , pointer required on the left side... etc etc,,,,,,
i have tried everythin but in vain.....
plz help me out......
Why don't you format the code using the # button to the right and post the compile errors.

For one thing, this line of code is not inside any function:
 
struct Data_Mesg Data=(struct Data_Mesg*) malloc(sizeof(struct Data_Mesg));


and Data is not even declared to be a pointer.
yes thanx man........
i got to knw that........
but why does

FILE * fd_N,*fd_PES;

//files are opened.........

while(!feof(fd_PES)) <- segmentation fault
{
k=0;
*str='\0';
while(!feof(fd_PES)) <- segmentation fault
*str++=fgetc(fd_PES);
.
.
.
.
.
..
.
.
give me segmentation fault ....??
i would really appreciate any help right now........
According to the above code, you are calling open() and storing the result in a FILE*. open() returns int; fopen() returns FILE*.

But since the above code clearly does not compile, I don't know what code you are actually running that is causing a segmentation fault.
ya actually i typecast it with (FILE *)open()
i have also tried fopen()
both compiles......
only that dreaded segmentation fault.............
Ok, simple rule to follow.

If you find that the compiler complains about something unless you typecast it, it almost always means the typecast is wrong.

Your typecast is wrong; if you want to use FILE*/feof, then you need to use fopen().
Topic archived. No new replies allowed.