473,395 Members | 1,678 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,395 software developers and data experts.

Problem when i pass pointers to functions

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);
}
Nov 11 '07 #1
15 1986
In article <fh**********@registered.motzarella.org>, ramif
<ra******@yahoo.co.ukwrote on Sunday 11 Nov 2007 9:42 pm:
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.
}

main()
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;
/* ... */

Nov 11 '07 #2
ramif wrote:
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.)
free(x);
}
--
Heapy Hedgehog
Meaning precedes definition.

Nov 11 '07 #3
Richard wrote:
ramif <ra******@yahoo.co.ukwrites:
>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.
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.

--
Historical Hedgehog
The "good old days" used to be much better.

Nov 11 '07 #4
ramif wrote:
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;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 11 '07 #5
Chris Dollin <eh@electrichedgehog.netwrites:
Richard wrote:
>ramif <ra******@yahoo.co.ukwrites:
>>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.

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.
Which does not apply to C. The word "reference" has a meaning in the
real world.
>
>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.
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.
>
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.
Nov 11 '07 #6
ramif wrote:
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.
Nov 11 '07 #7
Joe Wright <jo********@comcast.netwrites:
ramif wrote:
>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,
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.
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;
}
Nov 11 '07 #8
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.

--
IMAO Hedgehog
Nit-picking is best done among friends.

Nov 11 '07 #9
Chris Dollin <eh@electrichedgehog.netwrites:
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.
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".

Nov 11 '07 #10
ramif wrote:
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/infoce...ef/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?
Nov 11 '07 #11
ramif wrote:
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/infoce...ef/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?
Nov 11 '07 #12
ramif <ra******@yahoo.co.ukwrites:
>

http://publib.boulder.ibm.com/infoce...ef/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.
Nov 11 '07 #13
On Sun, 11 Nov 2007 22:51:51 +0100, ramif wrote:
>http://publib.boulder.ibm.com/infoce...ef/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?
The C version of 'swapnum' is obviously not correct.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Nov 11 '07 #14
On Sun, 11 Nov 2007 19:03:13 +0100, Richard wrote:
>Joe Wright writes:
>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.
Even "Java is strictly pass-by-value, exactly as in C":
http://javadude.com/articles/passbyvalue.htm
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Nov 11 '07 #15
Richard wrote:
Chris Dollin <eh@electrichedgehog.netwrites:
>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.

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.

--
Pointy Spines Hedgehog
"Who do you serve, and who do you trust?" /Crusade/

Nov 12 '07 #16

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

Similar topics

2
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); ...
1
by: TheOne | last post by:
I have two classes: class OntologyParser { ... protected: virtual void startElement(void *userData, const char *name, const char **atts); virtual void endElement(void *userData, const char...
7
by: Alan Holloway | last post by:
Hi all, Firstly thanks for your golden insight on my earlier post re bitwise operations. I now have another question! I've just been reading the excellent Peter van der Linden excerpt ...
6
by: Peter Oliphant | last post by:
Here is a simplification of my code. Basically, I have a class (A) that can be constructed using a function pointer to a function that returns a bool with no parameters. I then want to create an...
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)...
11
by: Michael | last post by:
Hi, I am trying to get an idea of how function pointers work. I have the following: #include <stdio.h> void do_stuff(int*,int,void*); void getInt(int*); void showInt(int*);
5
by: Amit_Basnak | last post by:
Dear Friends I am giving the following command to build the server executableon HP-UX /opt/aCC/bin/aCC -o SOAPServer SOAPServer.o WFWuListService.o WFWuListImpl.o WFWuListStructs.o WFWuList.o...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
11
by: venkatagmail | last post by:
I have problem understanding pass by value and pass by reference and want to how how they are or appear in the memory: I had to get my basics right again. I create an array and try all possible...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.