Problem when i pass pointers to functions

R

ramif

Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?

Here is my code:


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



// f() is supposed to change num's value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);
}



main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);
}
 
S

santosh

ramif said:
Does call by reference principle apply to pointers??

Strictly speaking there is no call by reference in C. It can however be
simulated with pointer values.
Is there a way to pass pointers (by reference) to functions?

Yes. Pass pointer to them.

int *p;
/* ... */
fx(&p);
/* ... */
Here is my code:

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

// f() is supposed to change num's value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);

A return would be better form here.

Implicit int is not legal anymore. Just declare main to return an int
and place a 'void' between the parenthesis to indicate that it takes no
arguments.
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);

And another return here.

What exactly do you want to do? Change the value held at the location
pointed to by 'x' or change what 'x' points to. To do the former f()
can just say:

*x = new_value;

To do the latter you need to enable f() to act on main's 'x' not just
it's local copy; pass the address of 'x' to do this:

f(&x);

/* in f() */
*x = new_address;
/* ... */
 
C

Chris Dollin

ramif said:
Does call by reference principle apply to pointers??

Yes. C doesn't have call by reference for anything, including
pointers. (The nearest thing it has is that arrays decay into
pointers in value context -- this can look like call by
reference, but it isn't, not least because it's nothing to
do with /calling/.)
Is there a way to pass pointers (by reference) to functions?

No.

What you /can/ do is to pass a /pointer-to-pointer-to/.
// f() is supposed to change num's value to 88...
void f(int *num)

void f( int **num )
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed

**num = 88;
num = num2; //data OUTSIDE function f() will not change...

*num = num2;
free(num2);

Hell's teeth, man, if you want the assignment to outside ths
function to mean anything, /don't free `num2`/.
}

main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x

f( &x );
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

It will now. (Of course, you've thrown away your only pointer
to the originally mallocated store. Oops.)
 
R

Richard

ramif said:
Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?

Someone will be along to say there is no call by reference. This is kind
of true. But misleading and confuses nearly everyone. You can pass a
pointer to an object which for all intents and meaning is a "reference"
for the greater majority of programmers. So for me, passing a pointer is
indeed pass by reference. No matter what the standard says : word games
dont get the point across.
Here is my code:


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



// f() is supposed to change num's value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);
}



main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

Not an error : you didn't change the contents of what the pointer num
points to in function f().

You probably want to pass a pointer to the pointer x.

f(&x);

And in f(int **num)

*num=newPointer;

But of course you will need another copy of x in order to call free.

Rather than repeat the same stuff over and over or rely on my accuracy
in non compilable examples, have a look here:

http://www.eskimo.com/~scs/cclass/int/sx8.html
 
C

Chris Dollin

Richard said:
Someone will be along to say there is no call by reference.
s/will/was/.

This is kind of true.

As in, true up to arguments about definitions.
But misleading and confuses nearly everyone.

It confuses people who have had it explained to them by confused
people.
You can pass a pointer to an object which for all
Most.

intents and meaning is a "reference" for the greater majority
of programmers.

Indeed.
So for me, passing a pointer is indeed pass by reference.

And here are the definitional things. No, passing a pointer
isn't pass (or call) by reference. You can do all the things
that pass by reference does by passing pointers, /except/
the doing-it-implicitly that's part of the definition of
pass-by-reference.
No matter what the standard says : word games
dont get the point across.

Exactly. Your confusion of terms doesn't make C have pass-
by-reference, in which the construction and dereference of
the necessary pointers is implicit and part of the machinery
of the language. C happens to have those constructions and
dereferences as directly accessible features, so it /doesn't
need/ to have pass-by-reference.

In a language like (the original) Pascal, you couldn't
simulate pass-by-reference with the rest of the language
features, and so it was much clearer that pass-by-reference
was a specific feature.
 
J

Joe Wright

ramif said:
Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?

Here is my code:


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



// f() is supposed to change num's value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);
}



main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);
}
C has no pass-by-reference no matter what anyone says. The C paradigm is
pass-by-value. That the value of a pointer may reference an object, we
still pass a value. I have altered your code example..

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

void f(int **num) {
**num = 88;
}

int main(void) {
int *x = malloc(sizeof (int));
if (x == NULL) exit(EXIT_FAILURE);
*x = 5;
printf("*x = %d\nExecuting f()...\n", *x);
f(&x); // calling f() to change the value of *x
printf("*x = %d\n", *x); // NO ERROR: *x DID CHANGE
free(x);
return 0;
}
 
R

Richard

Chris Dollin said:
As in, true up to arguments about definitions.


It confuses people who have had it explained to them by confused
people.


Indeed.


And here are the definitional things. No, passing a pointer
isn't pass (or call) by reference. You can do all the things
that pass by reference does by passing pointers, /except/
the doing-it-implicitly that's part of the definition of
pass-by-reference.

Which does not apply to C. The word "reference" has a meaning in the
real world.
Exactly. Your confusion of terms doesn't make C have pass-
by-reference, in which the construction and dereference of
the necessary pointers is implicit and part of the machinery
of the language. C happens to have those constructions and
dereferences as directly accessible features, so it /doesn't
need/ to have pass-by-reference.

As a reasonable C++ programmer in the past as well as other languages I
don't have a problem understanding to do with references in terms of
those languages. But in the C world talk of reference and dereference is
fairly clear cut IMO.

It is not goobledook to suggest in the English language that a pointer
references an object. It is just something I see time and time again and
I dont see the need to fight it.
 
R

ramif

ramif said:
Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?

Here is my code:


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



// f() is supposed to change num's value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);
}



main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);
}

thankx to all.
 
R

Richard

Joe Wright said:
C has no pass-by-reference no matter what anyone says. The C paradigm
is pass-by-value. That the value of a pointer may reference an object,

But you see in the real world we reference memory or objects or things.
I think if someone does not understand dereferencing a pointer then
being clever about passing by value when you KNOW they are talking about
passing a pointer or "reference" to an object is plain silly.
 
C

Chris Dollin

Richard wrote:
It is not goobledook to suggest in the English language that a pointer
references an object. It is just something I see time and time again and
I dont see the need to fight it.

Neither do I, and I'm not.

That doesn't make calling something "pass by reference" when it
isn't a wise idea: instead, it provides needless opportunities
for confusion.
 
R

Richard

Chris Dollin said:
Neither do I, and I'm not.

That doesn't make calling something "pass by reference" when it
isn't a wise idea: instead, it provides needless opportunities
for confusion.

I fully understand and agree with what and why you are saying. I just
don't think it should need to said - if some new C programmer asks about
pass by reference then (to me and I fully acknowledge I am very liberal
with language) I would say "Yes. There are more than one meaning of
"reference" but in C you can pass a pointer or "reference" to an object
by taking its address and passing that into a function". In 99.999% of
the times this is the functionality or access mechanism they are looking
for. I have no doubt I am wrong in terms of pure Computer Science (in
which I am qualified btw) or The C Standard - but in real life situations
I have never known anyone being confused by this representation. It's
plain common sense. This pointer "references" an "object". You
"dereference a parameter which is a pointer to an object".
 
R

ramif

ramif said:
Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?

Here is my code:


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



// f() is supposed to change num's value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);
}



main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);
}


http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr233.htm

The above website contains two articles of what IBM thinks about "pass
by reference" and "pass by value". (I am now a little bit confused...)

I have one final question: Does this website contains misleading
information?
 
R

ramif

ramif said:
Does call by reference principle apply to pointers??
Is there a way to pass pointers (by reference) to functions?

Here is my code:


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



// f() is supposed to change num's value to 88...
void f(int *num)
{
int *num2 = malloc( sizeof(int) );
*num2 = 88;

//*num = 88; //data OUTSIDE f() is changed
num = num2; //data OUTSIDE function f() will not change...

free(num2);
}



main()
{
int *x = malloc( sizeof(int));
*x = 5;

printf("x = %d\nExecuting f()...\n", *x);

f(x); //calling f() to change the value of x
printf("x = %d\n", *x); //ERROR: X DID NOT CHANGE

free(x);
}


http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr233.htm

The above website contains two articles of what IBM thinks about "pass
by reference" and "pass by value". (I am now a little bit confused...)

I have one final question: Does this website contains misleading
information?
 
R

Richard

ramif said:
http://publib.boulder.ibm.com/infoc.../com.ibm.xlcpp8a.doc/language/ref/cplr233.htm

The above website contains two articles of what IBM thinks about "pass
by reference" and "pass by value". (I am now a little bit
confused...)

It refers to what I said. Using a pointer to pass by reference. It's how
millions and millions of people in the real programming world think of
"pass by reference" when using C.

C does not "pass by reference" but it "allows pass by reference
through the use of a pointer to an object". Anything more picky is being
clever for clevers sake.
I have one final question: Does this website contains misleading
information?

IMO No.
 
C

Chris Dollin

Richard said:
I fully understand and agree with what and why you are saying.

Splendid! We rescue agreements from, er, something.
I just don't think it should need to said - if some new C programmer
asks about
pass by reference then (to me and I fully acknowledge I am very liberal
with language) I would say "Yes. There are more than one meaning of
"reference" but in C you can pass a pointer or "reference" to an object
by taking its address and passing that into a function".

I think our main difference here is that I'd start with "No, but" rather
than "yes". (I wouldn't use the same /words/ for the rest, but the spirit
would be much the same.)
In 99.999% of
the times this is the functionality or access mechanism they are looking
for.

99.9999% of statistics in newsgroups are made up, but I agree that
/usually/ they're looking to update a variable that appears as an
argument.
I have no doubt I am wrong in terms of pure Computer Science (in
which I am qualified btw)

It's just the traditional definition. Perhaps I'm too much the
traditionalist.
or The C Standard

I don't /think/ the Standard says anthing about call-by-reference
(since C doesn't have it [he said circularly] it doesn't need to).
- but in real life situations
I have never known anyone being confused by this representation. It's
plain common sense. This pointer "references" an "object". You
"dereference a parameter which is a pointer to an object".

That bit we'd already agreed, I thought.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top