LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-04-2009, 04:40 PM   #1
Tordne
Member
 
Registered: Mar 2008
Location: London,UK
Distribution: Ubuntu
Posts: 37

Rep: Reputation: 15
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')
{
}
}
 
Old 08-04-2009, 05:08 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Your

Code:
char *malloc();
is introduced by you prototype, and "char *" type contradicts the one that comes from a header file.
 
Old 08-04-2009, 05:18 PM   #3
Tordne
Member
 
Registered: Mar 2008
Location: London,UK
Distribution: Ubuntu
Posts: 37

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Sergei Steshenko View Post
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
 
Old 08-04-2009, 05:28 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Tordne View Post
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.
 
Old 08-05-2009, 08:36 PM   #5
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by Tordne View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
conflicting redeclaration of sys/types.h and linux/types.h schmil Programming 6 12-11-2008 02:02 PM
conflicting types for check_state nasim751 Programming 2 02-11-2008 12:58 AM
i get an error ./term.c :95 error conflicting types of tpam when compiling bitchx eveningblush Programming 0 03-30-2006 06:46 AM
error: conflicting types for `IPPROTO_IP' Anastasia Linux - Software 0 07-14-2005 06:12 AM
conflicting types error in parser.c snecklifter Programming 6 04-08-2005 06:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration