473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

pointers switch function

#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?

Nov 14 '05 #1
9 1968
apropo wrote:
#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?


Your function *should* switch the addresses pointed to by each pointer. So,
*x should return 5 after the function is called. However, your main()
procedure contains no way to test whether or not this is happening.

I think you want to write:
printf("x:%d,y:%d",*x,*y);
Nov 14 '05 #2
oops, and there's another problem. When you call your function, it creates
two new variables, a and b. a contains the value of x (a memory address), b
contains the value of y (also a memory address). When you switch them, a
contains the value of y and b contains the value of x. But then, when the
function quits, a and b are destroyed.

Here's an example of a replacement:

void tausche(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
}

If you do want to switch the actual addresses, you need to pass double
pointers.
Nov 14 '05 #3
> Your function *should* switch the addresses pointed to by each pointer. So,
*x should return 5 after the function is called. However, your main()
procedure contains no way to test whether or not this is happening.

I think you want to write:
printf("x:%d,y:%d",*x,*y);


you are right, but the code is still not as suposed to be, the _same_
output (?)

Nov 14 '05 #4
great, thanks a lot

J Baskin wrote:
oops, and there's another problem. When you call your function, it creates
two new variables, a and b. a contains the value of x (a memory address), b
contains the value of y (also a memory address). When you switch them, a
contains the value of y and b contains the value of x. But then, when the
function quits, a and b are destroyed.

Here's an example of a replacement:

void tausche(int *a,int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
}

If you do want to switch the actual addresses, you need to pass double
pointers.


Nov 14 '05 #5
apropo wrote:

#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
} i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?


It's essentially the same question as the one addressed in the FAQ:

http://www.eskimo.com/~scs/C-faq/q4.8.html

Brian Rodenborn
Nov 14 '05 #6
apropo wrote:

#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?


What are you two smoking? :-)

#include <stdio.h>
void tauche(int *a, int *b) {
int c = *a;
*a = *b;
*b = c;
}
int main(void) {
int x = 4, y = 5;
printf("x:%d, y:%d\n", x, y);
tauche(&x, &y);
printf("x:%d, y:%d\n", x, y);
return 0;
}
--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #7
apropo wrote:
#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?


Avoiding all the problems in your code with x and y not pointing to
anywhere useful and getch() not being a standard function:

#include <stdio.h>
#include <stdlib.h>

void tausche(int *a, int *b)
{
int c = *a;
*a = *b;
*b = c;
}
int main(void)
{
int x = 4, y = 5;
tausche(&x, &y);
printf("x:%d,y:%d\n", x, y);
getchar();
return 0;
}


--
Martin Ambuhl
Nov 14 '05 #8
J Baskin wrote:
apropo wrote:

#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?

Your function *should* switch the addresses pointed to by each pointer. So,
*x should return 5 after the function is called.


x and y point into outer space somewhere. *x and *y are meaningless when x
and y have themselves no useful meaning.
However, your main()
procedure contains no way to test whether or not this is happening.

I think you want to write:
printf("x:%d,y:%d",*x,*y);


That just adds another level of dereferencing uninitialized pointers.
--
Martin Ambuhl
Nov 14 '05 #9


apropo wrote:
#include <stdio.h>
#include <stdlib.h>
void tausche(int *a,int *b)
{
int *c=a;
a=b;
b=c;
}
int main(void)
{
int *x,*y;
*x=4;
*y=5;
tausche(x,y);
printf("x:%d,y:%d",x,y);
getch();
return 0;
}

i want this function - tausche - to switch the values stored at their
address, but it doesn't. why and how would it be correct ?


You're passing in pointers to your data, and then switch the POINTERS
inside the function. This is then lost when you exit the function. Once
you return to main(), the switching is irrelevant as you haven't
switched the DATA.

Perhaps:

void tausche(int* A, int* b)
{
int c = *a;
*a = *b;
*b = c;
}

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bryan Parkoff | last post by:
C/C++ Compiler encourages to limit 1,000 functions under function pointer, but I am allowed to overlimit 4,096 functions. I am just for fun to test how it works. The problem is that source code...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
45
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
5
by: Gary Wessle | last post by:
Hi I have a group of functions which have the same signature. void fun_n(void); according to a conditional structure "be it if-else or switch-case" I get to choose which one to run. ...
3
by: googlinggoogler | last post by:
Hi This should all be pretty standard C stuff, but I'm going to use terms like mouse callback to communicate what Im tyring to do. Basically I have my program whirling around in an infinite...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
25
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
2
by: Rahul | last post by:
Hi, I planning to replace the switch-case with an array of function pointers, the index for the array would be the case integers. Each case block would be implemented as a seperate function. It...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.