LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   GCC error: conflicting types for 'malloc' (https://www.linuxquestions.org/questions/programming-9/gcc-error-conflicting-types-for-malloc-745125/)

Tordne 08-04-2009 04:40 PM

GCC error: conflicting types for 'malloc'
 
Hi i'm learning to write some C code...
But I'm using book from 1999 here is the link... C tutorial

Anyway this is a really bad tutorial cause I had to find all the mistakes and faults in the code and I corrected them...
By the way if someone got a better book than this tell me...

But now I really can't find anything that will help me on the net.

Like I said in the title when i want to compile it, it still gives me the error for conflicting types for malloc()

Can someone give me some advice what goes wrong???

just copy/paste in emacs and try to compile it you'll see he blocks at line 21...
I can't run it through gdb because i got no executable file yet...

here it is

/* low level file handling */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <malloc.h>

#define CODE 0
#define SIZE 255
#define FNMSIZE 30
#define TRUE 1
#define FALSE 0
#define FAILED -1

#define CLRSCRN() system("clear")
#define NEWLINE() putchar('\n')

int fd;
char *malloc();


main ()

{char *data,getkey();

if ((data = malloc(SIZE)) == NULL)
{
puts("Out of memory\n");
return (CODE);
}

while (TRUE)
{
menu();
switch(getkey())
{
case 'l' : LoadFile(data);
break;
case 's' : SaveFile(data);
break;
case 'e' : EditFile(data);
break;
case 'd' : DeleteFile(data);
break;
case 'r' : RenameFile(data);
break;
case 'q' : if (sure())
{
return (CODE);
}
break;
}
}
}



menu ()

{
CLRSCRN();
printf("----------------------------\n");
printf("| MENU |\n");
printf("| ---- |\n");
printf("| |\n");
printf("| L) Load File |\n");
printf("| S) Save File |\n");
printf("| E) Edit File |\n");
printf("| D) Delete File |\n");
printf("| R) Rename File |\n");
printf("| Q) Quit |\n");
printf("| |\n");
printf("| Select option and RETURN |\n");
printf("| |\n");
printf("----------------------------\n");
NEWLINE();
}


LoadFile(data)

char *data;

{char *filename(),getkey();
int error;

fd = open(filename(), O_RDONLY);

if (fd == FAILED)
{
printf("File not found\n");
return (FAILED);
}

error = read(fd,data,SIZE);

if (error == FAILED)
{
printf("Error loading file\n");
return (FAILED);
}
else
{
if (error != SIZE);
{
printf("File is corrupted\n");
wait();
}
}

close(fd,data,SIZE);
return (error);
}



SaveFile (data)

char *data;

{char *filename(),getkey(),*fname;
int fd,error;

fd = open((fname = filename()), O_WRONLY);

if (fd == FAILED)
{
printf("File can not be written to\n");
printf("Create a new File? Y/N ");
if (yes())
{
if ((fd = CreateFile(fname)) == FAILED )
{
printf("Can not create file %s\n",fname);
return (FAILED);
}
}
else
{
return (FAILED);
}
}

error = write(fd,data,SIZE);

if (error < SIZE)
{
printf("Error writing to file\n");
if (error != FAILED)
{
printf("File only partially written\n");
}
}

close(fd,data,SIZE);
wait();
return (error);
}


EditFile (data)

char *data;

{char *ptr;
int ctr = 0;

printf("Contents of file :\n\n");

for (ptr = data; ptr < (data + SIZE); ptr++)
{
if (isprint(*ptr))
{
putchar(*ptr);
if ((ctr++ % 60) == 0)
{
NEWLINE();
}
}
}
printf("\n\nEnter %1d characters: \n",SIZE);

for (ptr = data; ptr < (data +SIZE); ptr++)
{
*ptr = getchar();
}

skipgarb();
}


DeleteFile ()

{ char *filename(),getkey(),*fname;

printf("Delete File\n\n");

fname = filename();
if (sure())
{
if (remove(fname) == FAILED)
{
printf("Can't delete %s\n",fname);
}
}
else
{
printf("File NOT deleted!\n");
}
wait();
}



RenameFile()

{char old[FNMSIZE],*new;

printf("Rename from Old to New\n\nOld : ");
strcopy(old,filenm());
printf("\nNew : ");
*new = filenm();

if (rename(old,new) == FAILED)
{
printf("Can't rename %s as %s \n",old,new);
}
wait();
}



CreateFile (fname)

char *fname;

{int fd;

if((fd = creat(fname,0)) == FAILED)
{
printf("Can't create %s\n",fname);
return (FAILED);
}

return (fd);
}



char *filename()

{ static char statfilenm[FNMSIZE];

do
{
printf("Enter Filename : ");
scanf("%24s",statfilenm);
skipgarb();
}
while (strlen(statfilenm) == 0);
return (statfilenm);
}


sure ()

{
printf("Are you sure? Y/N ");
return (yes());
}


yes ()

{char getkey();

while (TRUE)
{
switch(getkey())
{
case 'y' : return (TRUE);
case 'n' : return (FALSE);
}
}
}



wait ()

{char getkey();

printf("Press any key\n");
getkey();
}



char getkey()

{char ch;

ch = getchar();
skipgarb();
return ((char)tolower(ch));
}



skipgarb ()

{
while (getchar() != '\n')
{
}
}

Sergei Steshenko 08-04-2009 05:08 PM

Your

Code:

char *malloc();
is introduced by you prototype, and "char *" type contradicts the one that comes from a header file.

Tordne 08-04-2009 05:18 PM

Quote:

Originally Posted by Sergei Steshenko (Post 3631504)
Your

Code:

char *malloc();
is introduced by you prototype, and "char *" type contradicts the one that comes from a header file.

and do you know a solution for it, cause this is already a modified version of the book, i tried to take it away if you say it cotradicts but then i get some other errors...

How can i modify the code to make it work?

Anyway thanks for the response

Sergei Steshenko 08-04-2009 05:28 PM

Quote:

Originally Posted by Tordne (Post 3631512)
and do you know a solution for it, cause this is already a modified version of the book, i tried to take it away if you say it cotradicts but then i get some other errors...

How can i modify the code to make it work?

Anyway thanks for the response

Just remove your prototype.

And on Linux 'malloc' is declared in 'stdlib.h' file; so

Code:

#include <malloc.h>
is redundant - remove it too if you're on Linux.

Dan04 08-05-2009 08:36 PM

Quote:

Originally Posted by Tordne (Post 3631483)
Hi i'm learning to write some C code...
But I'm using book from 1999 here is the link... C tutorial

The book may have been written in 1999, but the code looks like it was written in 1980. I'm not surprised you're having trouble compiling it.


All times are GMT -5. The time now is 01:46 PM.