LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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-14-2006, 02:01 PM   #1
kpachopoulos
Member
 
Registered: Feb 2004
Location: Athens, Greece
Distribution: Gentoo,FreeBSD, Debian
Posts: 705

Rep: Reputation: 30
c compilation error


Hi,
here's the code:
Code:
#include <stdlib.h>
#include <stdio.h>

FILE* source;
char c;

main(int argc, char *argv[])
{
	if (argc==2)
	{	
		source=fopen(argv[1],"r");
		if (source==NULL) perror("error opening file");
	
		print_output(source);
	}
	
}

print_output(FILE* f)
{		
	//char* buf_pointer;

	while (c!=EOF)
	{
		//buf_pointer=do_buffer(f);
		printf("%s", (char*) (do_buffer(f)));
	}

	printf("\n");
}

char* do_buffer(FILE* f)
{
	char buffer[80];
	int i=0;
	initialize_buffer(&buffer);

	while ((i<79) && (c!=EOF))
	{
		c=(char)fgetc(source);
		buffer[i]=c;
		i++;
	}

	return buffer;
}

initialize_buffer(char b[])
{
	int current_pos=0;
	for (;current_pos<sizeof(b); current_pos++)
	{
		b[current_pos]=NULL;
	}
}
And this is the output, when i try to compile it:
Code:
kostas@vakhos:~$ gcc -o cp cp.c
cp.c:33: error: conflicting types for ‘do_buffer’
cp.c:26: error: previous implicit declaration of ‘do_buffer’ was here
cp.c: In function ‘do_buffer’:
cp.c:45: warning: function returns address of local variable
cp.c: In function ‘initialize_buffer’:
cp.c:53: warning: assignment makes integer from pointer without a cast
cp.c:66:2: warning: no newline at end of file
What's the problem?
Thanks
 
Old 08-14-2006, 02:18 PM   #2
a.malich
LQ Newbie
 
Registered: Aug 2006
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
Hi,

Quote:
cp.c: In function ‘do_buffer’:
cp.c:45: warning: function returns address of local variable
You declared buffer inside the do_buffer function and then you try to return its address - while buffer is a local variable, it's gone when the function has finished, and the value do_buffer returned points to an invalid address.

Simply declare buffer globally (like you did with c).

Quote:
cp.c: In function ‘initialize_buffer’:
cp.c:53: warning: assignment makes integer from pointer without a cast
(This is a guess) NULL is an integer value, while b is an array of type char. Replace NULL with '\0' (backslash zero), which is a char.
Code:
b[current_pos]='\0';
That _should_ do it.
Quote:
cp.c:66:2: warning: no newline at end of file
Simply go to the very end of the file and hit Enter.

Don't know about the rest (yet).

Also, functions that do not return anything should be declared like this:
Code:
void i_dont_return_anything(int number);
a.malich

Last edited by a.malich; 08-14-2006 at 02:41 PM.
 
Old 08-14-2006, 02:35 PM   #3
zhangmaike
Member
 
Registered: Oct 2004
Distribution: Slackware
Posts: 376

Rep: Reputation: 31
For the implicit-declaration thing, you use do_buffer() in main() before you declare it. This is bad. Use a function prototype, like this:
Code:
char* do_buffer(FILE*);
The same should be done for your other functions. Place them in your global declarations, before main().

By the way, why is source declared globally, when you are passing it as a parameter? Generally, it is considered better to pass variables as parameters than to define global ones. Same thing with the char c. Although it isn't used as a parameter, you'll have to be very careful to avoid accidentally clobbering c by a calling a function which also uses c. A warning: c is being used uninitialized in print_output() when first called from main().

As far as declaring buffer as global to solve the returning-address-of-local-variable problem in do_buffer(), the usual way of doing this is to avoid global variables and pass a char* as a parameter to do_buffer(), and declare the buffer itself in the routine that calls do_buffer()... that is, declare char buffer[80] in print_output(), and pass it as a parameter to do_buffer(). If you do it this way, you'll have to add a char* parameter to both the function definition and the prototype.

Quote:
NULL is an integer value, while b is an array of type char. Replace NULL with '\0' (backslash zero), which is a char.
Sort of. NULL has the type of void*. Assigning a pointer type to any integer type always gives a warning. Using 0 or '\0' will solve this problem.

Last edited by zhangmaike; 08-14-2006 at 02:51 PM.
 
Old 08-14-2006, 02:52 PM   #4
a.malich
LQ Newbie
 
Registered: Aug 2006
Distribution: Slackware
Posts: 9

Rep: Reputation: 0
Thanks for clearing some things up, zhangmaike. I only skimmed through the code, hence I thought that main() is the 'caller' of do_buffer. I never would've thought NULL is void*, though. :)

a.malich

Last edited by a.malich; 08-14-2006 at 02:53 PM.
 
  


Reply



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
Kernel compilation error: Error 15: File not found Niceman2005 Linux - General 9 10-04-2007 03:45 AM
compilation error Varadharajan Programming 9 02-04-2007 01:56 AM
Compilation error elgitaro Programming 1 03-14-2005 11:50 PM
Compilation Error abdullahgee Programming 9 05-21-2004 12:29 AM
Sqwebmail compilation error ... [maildirsearchC.o] Error 1 boogie_maan Linux - Software 0 10-26-2002 07:21 PM

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

All times are GMT -5. The time now is 12:03 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
Open Source Consulting | Domain Registration