About pointer

W

wilson

Hi All,

I am a novice at C and just have learned pointer for a short period.
Today one error had occured while executing my program below.
And I cannot find the error out since it's checked "OK" by Dev C++.

Here is:(I want to exchange the value of "a" and "b" with function "swap")

void swap(int *pa, int *pb)
{
int *tmp;
*tmp = *pa;
*pa = *pb;
*pb = *tmp;
}
// ---------------------------
int main()
{
int a=10, b=20;

printf("a=%d b=%d\n",a,b);
swap(&a, &b);
printf("a=%d b=%d\n",a,b);

system("PAUSE");
return 0;
}
 
J

Joona I Palaste

wilson said:
I am a novice at C and just have learned pointer for a short period.
Today one error had occured while executing my program below.
And I cannot find the error out since it's checked "OK" by Dev C++.
Here is:(I want to exchange the value of "a" and "b" with function "swap")
void swap(int *pa, int *pb)
{
int *tmp;
*tmp = *pa;

This pointer isn't pointing anywhere, yet you indirect through it. This
causes undefined behaviour.
*pa = *pb;
*pb = *tmp;
}

The canonical way to write a swap function is to use a normal int for
the tmp value, not an int pointer. Here is the canonical way:

void swap(int *pa, int *pb)
{
int tmp;
tmp = *pa;
*pa = *pb;
*pb = tmp;
}
 
C

Christopher Benson-Manica

wilson <[email protected]> spoke thus:

Prefer Joona's way, but understand the following changes to the code
you posted:
void swap(int *pa, int *pb)
{
int *tmp;
tmp=malloc( sizeof *tmp ); /* 1 */
if( tmp ) { /* 2 */
*tmp = *pa;
*pa = *pb;
*pb = *tmp; }
}

1 - tmp does not point at anything initially, as Joona said. This
line makes it point at memory that is the size of an int (sizeof *tmp
= "the size of the object that tmp points to").

2 - malloc() may return NULL; this makes sure that it did not before
attempting to go on.
 
E

Emmanuel Delahaye

wilson wrote on 10/08/04 :
void swap(int *pa, int *pb)
{
int *tmp;

What could this pointer be used for?
*tmp = *pa;

How is it initialized ?

Think more about the purpose of the tmp variable.
*pa = *pb;
*pb = *tmp;
}
// ---------------------------
int main()
{
int a=10, b=20;

printf("a=%d b=%d\n",a,b);
swap(&a, &b);
printf("a=%d b=%d\n",a,b);

system("PAUSE");
return 0;
}

BTW, You missed <stdio.h> and <stdlib.h>
 
R

RoSsIaCrIiLoIA

Hi All,

I am a novice at C and just have learned pointer for a short period.
Today one error had occured while executing my program below.
And I cannot find the error out since it's checked "OK" by Dev C++.

Here is:(I want to exchange the value of "a" and "b" with function "swap")

void swap(int *pa, int *pb)
{
int *tmp;
*tmp = *pa;
*pa = *pb;
*pb = *tmp;
}

if you wanna use a function for swap 2 address I see a problem
swap(&pa, &pb); change only the values in the stack of &pa and &pb not
the value of &pa and &pb seen in main()
if I write
void swap(int **pa, int **pb)
{
int *tmp;
*tmp = *pa;
*pa = *pb;
*pb = *tmp;
}
seems there are problems for the compiler in the instruction
swap(&(&a), &(&b));
so here it is my solution (note system("PAUSE"); is not portable
through the OSes (operative systems))

#include <stdio.h>
#define swap( a, b) do{ tmp=a; a=b; b=tmp; }while(0)


int main(void)
{
int a=10, b=20, tmp;

printf("a=%d b=%d\n",a,b);
swap( a, b );
printf("a=%d b=%d\n",a,b);

printf("Inserisci un carattere per finire... "); fflush(stdout);
getchar();
printf("\n");

return 0;
}
 
R

RoSsIaCrIiLoIA

if you wanna use a function for swap 2 address I see a problem
swap(&pa, &pb); change only the values in the stack of &pa and &pb not
the value of &pa and &pb seen in main()

wrong I would swap the addres and not the values. This should be ok:

#include <stdio.h>

void swap(int* a, int* b)
{int temp;
temp = *a;
*a = *b;
*b = temp;
}


int main(void)
{
int a=10, b=20;

printf("a=%d b=%d\n",a,b);
swap( &a, &b );
printf("a=%d b=%d\n",a,b);

printf("Inserisci un carattere per finire... "); fflush(stdout);
getchar();
printf("\n");

return 0;
}
 
J

Joona I Palaste

I to am just a novice but the function prints or does nothing
the function has no output

Yes it does something. Because it assigns to *pa and *pb rather than pa
and pb, the changes will be visible in the variables a and b in the
calling function.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
D

Darklight

This works for me

#include<stdio.h>

void swap(int *pa, int *pb)
{
int *tmp, *tmp1;

tmp = pb;
pb = pa;
tmp1 = pb;

printf("a = %d b = %d\n",*tmp, *tmp1);

}

int main(void)
{
int a = 10, b = 20;

printf("a = %d b = %d\n",a,b);
swap(&a, &b);
printf("a = %d b = %d\n",a,b);

return 0;
}

I can't explain it but you can do the same thing without the pointers
now i will wait to get burned
 
E

Emmanuel Delahaye

Darklight wrote on 10/08/04 :
This works for me

#include<stdio.h>

void swap(int *pa, int *pb)
{
int *tmp, *tmp1;

tmp = pb;
pb = pa;
tmp1 = pb;

printf("a = %d b = %d\n",*tmp, *tmp1);

}

int main(void)
{
int a = 10, b = 20;

printf("a = %d b = %d\n",a,b);
swap(&a, &b);
printf("a = %d b = %d\n",a,b);

return 0;
}

I can't explain it but you can do the same thing without the pointers
now i will wait to get burned

Ok, do it and come and cry!
 
W

wilson

Thank you all.

It works as below:

/*****************
int tmp;
tmp = *pa;
*pa = *pb;
*pb = tmp;
...
*****************/

Thanks and Regards.
 
K

Kieran Simkin

Michael Yang said:
Darklight <[email protected]> scribbled the following:
[snip]
It should work below

void swap(int* pa, int* pb){
int* tmp;
tmp=pa;
pa=pb;
pb=tmp;
}


swap(&a, &b);

Wrong. C passes by function arguments by value. See below:

$ cat swaptest.c
#include<stdio.h>
void swap (int *pa, int *pb);
int main (void) {
int a=1,b=2;
printf("a=%i,b=%i\n",a,b);
swap(&a,&b);
printf("a=%i,b=%i\n",a,b);
return 0;
}
void swap(int* pa, int* pb){
int* tmp;
tmp=pa;
pa=pb;
pb=tmp;
}

$ gcc swaptest.c -o swaptest
$ ./swaptest
a=1,b=2
a=1,b=2
 
C

CBFalconer

Joona said:
Yes it does something. Because it assigns to *pa and *pb rather than pa
and pb, the changes will be visible in the variables a and b in the
calling function.

It should blow up. ALL the *tmp should be just tmp.
 
M

Martin Ambuhl

wilson said:
Hi All,

I am a novice at C and just have learned pointer for a short period.
Today one error had occured while executing my program below.
And I cannot find the error out since it's checked "OK" by Dev C++.

Here is:(I want to exchange the value of "a" and "b" with function "swap")

void swap(int *pa, int *pb)
{
int *tmp;
^^^^^^^^ int tmp; /* you need an int to store *pa in; 'int *tmp'
declares a pointer, and it doesn't even point
anywhere because it's not initialized. You
could keep your pointer, but only at the
expense of additional code after 'int *tmp;',
for example:
int foobar;
tmp = &foobar;
*/
 
E

Emmanuel Delahaye

Michael Yang wrote on 11/08/04 :
void swap(int* pa, int* pb){

Why do you think a pointer is used here ?
int* tmp;

What for is this pointer?
tmp=pa;
pa=pb;
pb=tmp;

Obviously, you have to work harder on the 'pointers' topic. You are
100% wrong. Modifiying the value of a parameter has no effet on the
original value.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top