LinuxQuestions.org
Review your favorite Linux distribution.
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 01-05-2011, 03:13 PM   #1
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Rep: Reputation: 21
Programming help


HI All,
I have written a simple C program in which I dont feel anything is wrong but still the GCC compiler is giving errors and warning.Can someone help me find out whats exactly going wrong in here.
The errors are
Quote:
prog2.c:3: error: syntax error before '*' token
prog2.c: In function `swap':
prog2.c:4: error: number of arguments doesn't match prototype
prog2.c:2: error: prototype declaration
prog2.c:6: error: `x' undeclared (first use in this function)
prog2.c:6: error: (Each undeclared identifier is reported only once
prog2.c:6: error: for each function it appears in.)
prog2.c:7: error: `y' undeclared (first use in this function)
And the program is

Quote:
#include<stdio.h>
int swap(int *,int *);
int swap(*x,*y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
int main(void)
{
int a=10,b=20;
swap(&a,&b);
printf("a=%d",a);
printf("b=%d",b);
return 0;
}
Thanks!
 
Old 01-05-2011, 03:35 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
I've reported this to be moved to Programming, since it's more suitable there.

First of all, the prototype

int swap(int *,int *);

is unnecessary, since you're defining the function before main().

Secondly, the line

int swap(*x,*y)

is incorrect, as you've not given types for the parameters x and y, other than specifying that they're pointers. Edit: also, surely this function should return void, as it doesn't return anything.

Edit: also, in future, put your code between CODE tags, rather than QUOTE tags, as the former preserve indentation.

Last edited by Nylex; 01-05-2011 at 03:55 PM.
 
Old 01-05-2011, 04:09 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Nylex View Post
First of all, the prototype

int swap(int *,int *);

is unnecessary, since you're defining the function before main().
I think that is a matter of style, not an error, so I hope your opinion on that didn't distract Ajit Gunge from understanding the important info you gave next.

Quote:
the line

int swap(*x,*y)

is incorrect, as you've not given types for the parameters x and y, other than specifying that they're pointers.
That is the major error.

Quote:
surely this function should return void, as it doesn't return anything.
Correct, but I think most compilers give just a warning, not an error for that mistake.

Quote:
also, in future, put your code between CODE tags
I'll repeat that as well.

In addition:

Code:
   printf("a=%d",a);
   printf("b=%d",b);
I expect the OP will understand the minor error in those lines better by seeing the output of the program before correcting that error, than if I pointed it out now.

Quote:
Originally Posted by Ajit Gunge View Post
Thanks!
I think it is more polite to save that for the post you put at the end of the thread telling us that you understood our answers and your program is now working.

Obviously, feel free to first post follow up questions if you didn't understand the answers or your program still doesn't work. Though unfortunately common, it is impolite to skip posting a final status (it worked or you gave up).

Last edited by johnsfine; 01-05-2011 at 04:17 PM.
 
1 members found this post helpful.
Old 01-05-2011, 04:13 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Quote:
Originally Posted by johnsfine View Post
I think that is a matter of style, not an error, so I hope your opinion on that didn't distract Ajit Gunge from understanding the important info you gave next.
Fair enough.

Quote:
Correct, but I think most compilers give just a warning, not an error for that mistake.
True. I'm used to compiling with -Wall with gcc.
 
Old 02-09-2011, 06:36 AM   #5
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253

Original Poster
Blog Entries: 1

Rep: Reputation: 21
Thanks for your comments.The program has worked now.Sorry for the delay though.
 
Old 02-09-2011, 08:38 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Mark the thread as solved.
 
Old 02-09-2011, 01:01 PM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Ajit Gunge View Post
Code:
int swap(*x,*y)
 {
 int t;
 t=*x;
 *x=*y;
 *y=t;
 }
The pre-standard style of argument definition would have you do it like this:
Code:
int swap(x,y)
	int *x;
	int *y;
 {
 int t;
 t=*x;
 *x=*y;
 *y=t;
 }
(Don't copy that! C hasn't been like that since the 80s.) Did you, by chance, learn from an old reference?
Kevin Barry
 
  


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



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

All times are GMT -5. The time now is 08:01 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