Question:
What is error in this 'C' language program?
A
2009-04-11 22:45:55 UTC
what is the error in this program?
this prog is of swap function it is used to exchangevalues of two no.s:

#include
#include
void main()
{
int a, b;
int(int x*, inty*);
a=100;
b=200;
printf("before exchanging the values of a & b:%d%d",a,b);
swap(&a,&b);
printf("after exchanging the values of a & b:%d%d", a,b);
getch( );
}
int(int x*, inty*);
{
int t;
x*=y*;
y*=t;
x*=t;
}
Six answers:
Kendall Sylvan
2009-04-11 22:58:44 UTC
you didn't name your function at all it should be named 'swap'...



also, a swap function is a 'void' type, not an int, because it doesn't return anything.



by the way, you can write swap as:



void swap( int *x, int *y) {



*x ^= *y;

*y ^= *x;

*x ^= *y;

}



---



when you call the function a lot, it runs much quicker because you aren't creating and destroying a variable each time on the system stack.



..
anonymous
2009-04-11 23:32:53 UTC
----- THE SWAP FUNCTION -----

Functions must be declared before they are called. Since you call your function with "swap(&a,&b)" you need to either declare the swap function, or move it up before the main function. In my implementation, I moved it.



The swap function must be named (you never gave it a name, it won't know what it is supposed to be calling), and there isn't anything obvious for it to return, so I changed it's type to void.



You have a semicolon after the swap function's signature, this ends the method (good for a declaration, but if you are defining your function, this says "I'm done" and it won't know to keep looking for the rest of your function), so remove this semicolon.



With pointers, the asterisks go before the variable (otherwise it looks like "x times comma", which makes no sense).



In your swap function declaration, you need a space between int and y, it does not know what "inty" is.



Your swap function logic is wrong, if you overwrite the value of x before storing it somewhere, how will you be able to assign it to y? You need to save it in t before overwriting it.





----- THE MAIN FUNCTION -----

int(int x*, inty*); doesn't make any sense, remember you have to name your functions. You cannot just duplicate some function's signature in your main program and expect it to work. (plus, you haven't assigned values at this point, so why would you even be swapping them anyway?) remove this line completely.



I also added spacing and a comma in your printf statements so you can tell where a ends and b begins. And you need a newline in your first print statement, so that these two statements are on their own lines, otherwise they run together.



getch(); is unneccessary. I assume it is just a method for pausing the program, so you can evaluate it, a trick I also use for my crappy IDE I use in Windows.



Declaring main as void will cause some compilers to complain. I prefer to declare it as an int. To go with this, you would need to return an integer value at the end, zero is standard for a program that executed with no errors.





----- OTHER THOUGHTS -----

You don't need conio.h, it's more for convenient input/output. But you should avoid using it in general, because if you write a program which relies on it's functions, your program won't work on non-Windows platforms.



I strongly advise that in the future, you only complete one small portion of your program, then test that it works how you expect before adding in anything else. This program was clearly written all at once, and you aren't capable enough to do something like this, you probably couldn't debug it because there were so many bugs that you can't even figure out where to start. Bugs within and on top of other bugs. Plus, your use of getch(); implies you are using a crappy IDE/compiler that will spit out a thousand errors for each of these when you go to compile, and you won't be able to glean any useful information off of it. If you test as you go, then you can always know what line specifically is wrong, and it is much easier to troubleshoot. Also, if you can use a Linux machine while you are learning, gcc tends to be much more informative and easy to read. If you are using MS Visual C++ , you should look for something better, I use Dev-C++ which you can get at http://www.bloodshed.net/ It isn't as good as a simple text editor and Linux, but it is much better than the Microsoft products. At least in my anecdotal experience.
yufongwe
2009-04-11 23:08:01 UTC
A lot of stuff.



1) For pointers, the asterisk comes before the variable name, not after.



2) You need to write a prototype for your swap function before you call main(). You also need to write its signature when you're defining it.



3) You should not have a semicolon when you're defining the swap function below main()



4) WTF is int(int x*, int y*); ? I don't even have the faintest clue what you're thinking there.



5) The logic of your swap function is flawed. You need to rewrite it. I hope you can figure out why this is.
anonymous
2009-04-11 22:59:44 UTC
Use this:



#include

#include

void swap( int x*, int y*);

void main()

{

int a, b;

a=100;

b=200;

printf("before exchanging the values of a & b:%d%d",a,b);

swap(&a,&b);

printf("after exchanging the values of a & b:%d%d", a,b);

getch( );

}

void swap( int x*, int y*)

{

int t;

x*=y*;

y*=t;

x*=t;

}
hogge
2016-10-18 11:36:38 UTC
your assigning value the incorrect way. you certainly in basic terms pronounced, "set value to (??? < 500)", it can't additionally be interpretted as a boolean the form you have worded it simply by fact it would not be attentive to what to income 500 to. i could supply you an occasion of the splendid thank you to do it if I knew what you're attempting to do...ASCII values are a million byte and selection from 0-255, so what with the value =< 500?
anonymous
2009-04-11 22:55:36 UTC
It looks like you're using "inty*" instead of "int y*".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...