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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-05-2011, 03:13 PM
|
#1
|
Member
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Rep:
|
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!
|
|
|
01-05-2011, 03:35 PM
|
#2
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464
Rep: 
|
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.
|
|
|
01-05-2011, 04:09 PM
|
#3
|
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286
|
Quote:
Originally Posted by Nylex
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
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.
|
01-05-2011, 04:13 PM
|
#4
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464
Rep: 
|
Quote:
Originally Posted by johnsfine
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.
|
|
|
02-09-2011, 06:36 AM
|
#5
|
Member
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Original Poster
Rep:
|
Thanks for your comments.The program has worked now.Sorry for the delay though.
|
|
|
02-09-2011, 08:38 AM
|
#6
|
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
|
Mark the thread as solved.
|
|
|
02-09-2011, 01:01 PM
|
#7
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
Quote:
Originally Posted by Ajit Gunge
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
|
|
|
All times are GMT -5. The time now is 07:36 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|