Pointers are pased by copying ?

A

arnuld

Excellent! It words the same with a pointer -- provided you think
about the value of the pointer itself


okay, you mean pointers are passed by copying. I have cooked a little
program to understand it:


#include <stdio.h>

enum { ARRSIZE = 2 };

void swap_ptr( int*, int* );


int main( void )
{
int x, y;
int *px, *py;

x = 1;
y = -1;


px = &x;
py = &y;


printf("px = %p, *px = %d\t", (void*)px, *px);
printf("py = %p, *py = %d\n", (void*)py, *py);

swap_ptr( px, py );

printf("px = %p, *px = %d\t", (void*)px, *px);
printf("py = %p, *py = %d\n", (void*)py, *py);

return 0;
}



void swap_ptr( int* pa, int* pb )
{
int* temp;

temp = pa;
pa = pb;
pb = temp;

}

================= OUTPUT ========================
[arnuld@dune ztest]$ gcc -ansi -pedantic -Wall -Wextra test.c
[arnuld@dune ztest]$ ./a.out
px = 0xbff5b244, *px = 1 py = 0xbff5b240, *py = -1
px = 0xbff5b244, *px = 1 py = 0xbff5b240, *py = -1
[arnuld@dune ztest]





you see, pointers are not getting swapped. It means, *everything* in C is
passed as a copy, rather than a reference. Even arrays are not exception
because they should be passed as a copy but the gist of C is that when you
pass an array , C silently passes a pointer to the first element of the
array which can change the original array elements. The pointer himself is
passed as copy. If you want to change the pointer itself, then you need
to pass a pointer to this pointer. I don't think I ever read this in any C
book, purely practical experience :)

Am I right ?
 
I

Ian Collins

arnuld said:
okay, you mean pointers are passed by copying. I have cooked a little
program to understand it:
Pointers are no different form any other type, they are passed by value.
Just like any other type, if you want to change the value of a pointer
with a function, you must pass its address.
 
A

Andrew Poelstra

you see, pointers are not getting swapped. It means, *everything* in C is
passed as a copy, rather than a reference. Even arrays are not exception
because they should be passed as a copy but the gist of C is that when you
pass an array , C silently passes a pointer to the first element of the
array which can change the original array elements. The pointer himself is
passed as copy. If you want to change the pointer itself, then you need
to pass a pointer to this pointer. I don't think I ever read this in any C
book, purely practical experience :)

Am I right ?

Yes, you are.

Though I'm sure it's adequately covered in some C book somewhere..
 
K

Kenny McCormack

Yes, you are.

Though I'm sure it's adequately covered in some C book somewhere..

Right. Wouldn't want to, you know, like actually allow someone to feel
good about themselves. Not here. Not in CLC.
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top