472,326 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

pass by reference is valid?

I've read that C allows two ways to pass information between
functions:

o Pass by Value
o Pass by Reference

I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?

Also I read that, "C does not have run-time typing". What does this
mean and is it true?

C just cares that you pass the correct type of arguments (int, float,
char, ...) and doesn't care about the value or variable/data structure
name is that also correct?

I wrote 3 simple test programs for the different ways to pass
information in functions. Any critiques/improvements would be welcome:
/* Example 1 */

/* func.c */

int swap(int a, int b)
{
a = b;
return a;
}

/* main.c */

int b =1;
int c = 2;

int main(void)
{

swap(b,c);
printf("%d",c);
return 0;
}

BTW in my example of func.c and main.c here it doesn't seem to matter
if I make b, c global variables or local. In writing C how do I best
decide if I should make variables global or local, or if it won't
affect the purpose of the code should I not care?

/* Example 2 */

/* func2.c */

int swap(int &a, int &b)
{
a = b;
return a;
}

/* main2.c */

int *b =1;
int *c = 2;

int main(void)
{

swap(&b,&c);
printf("%d",&c);
return 0;
}

/* Example 3 */

/* func3.c */

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

/* main3.c */

int *b =1;
int *c = 2;

int main(void)
{

swap(*b,*c);
printf("%d",&c);
return 0;
}

PS: I'm learning about closures in Lisp and they seem nifty, but I
read C doesn't have them. Or is this not quite true?

Lisp 9000

Sep 15 '07 #1
6 2583
li******@gmail.com wrote, On 15/09/07 08:55:
I've read that C allows two ways to pass information between
functions:

o Pass by Value
o Pass by Reference
You may not have read the book carefully enough, or it may
be a bad book.
I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
The C programmers are correct. Of course, passing a pointer by value
allows you to do something very similar to passing by reference
foo(&i);
foo can now modify the value of i because you passed a pointer to it.
Also I read that, "C does not have run-time typing". What does this
mean and is it true?
In some languages you can do something like:
func(var)
if var is an int then do something
else if var is a string do something else

In C you can't.
C just cares that you pass the correct type of arguments (int, float,
char, ...) and doesn't care about the value or variable/data structure
name is that also correct?
There are some promotions which are automatically performed, so the
above is at least misleading.
I wrote 3 simple test programs for the different ways to pass
information in functions. Any critiques/improvements would be welcome:

/* Example 1 */

/* func.c */

int swap(int a, int b)
{
a = b;
return a;
}
Why make swap return an int? I would say that void makes more sense.
Obviously it does not work as you know.
/* main.c */

int b =1;
int c = 2;

int main(void)
{

swap(b,c);
printf("%d",c);
return 0;
}

BTW in my example of func.c and main.c here it doesn't seem to matter
if I make b, c global variables or local. In writing C how do I best
decide if I should make variables global or local, or if it won't
affect the purpose of the code should I not care?
The simple rule for beginners is make them local. When you know a bit
more you make them local unless you have a *very* good reason not to.
The reason for this is that if a variable is local you can easily see
where it is modified. If it is global you have to check the entire
1,000,000 line program to see what affects it.
/* Example 2 */

/* func2.c */

int swap(int &a, int &b)
A syntax error in C. If you compiler accepts it then it is not working
as a C compiler.

<snip>
/* Example 3 */

/* func3.c */

int swap(int *a, int *b)
{
*a = *b;
return *a;
}
Again, I would not return a value.
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
You should be able to work out the reasons for the rest of the changes
since you called the function 'swap' not 'assign'.
/* main3.c */

int *b =1;
int *c = 2;
No, no, a thousand time no. You want int variables, not pointers to int.
A pointer points somewhere, the value it points to is held somewhere
else and you have to provide that place.
int main(void)
{

swap(*b,*c);
With b and c being pointers, this would pass the value pointed to (if
the rest was correct) *not* the pointer value. The names of the
variables are 'b' and 'c' *not* '*b' and '*c'.
printf("%d",&c);
I don't even know what you were thinking.
return 0;
}
int main(void)
{
int b=2;
int c=3;
printf("%d %d\n",b,c);
swap(&b,&c);
printf("%d %d\n",b,c);
return 0;
}

You use & to get a pointer to a variable.
PS: I'm learning about closures in Lisp and they seem nifty, but I
read C doesn't have them. Or is this not quite true?
C does not have them.

Personally I think trying to learn two languages at once is a mistake.
--
Flash Gordon
Sep 15 '07 #2
On Sat, 15 Sep 2007 07:55:47 +0000, li******@gmail.com wrote:
I've read that C allows two ways to pass information between
functions:

o Pass by Value
o Pass by Reference

I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?

Also I read that, "C does not have run-time typing". What does this
mean and is it true?

C just cares that you pass the correct type of arguments (int, float,
char, ...) and doesn't care about the value or variable/data structure
name is that also correct?

I wrote 3 simple test programs for the different ways to pass
information in functions. Any critiques/improvements would be welcome:
/* Example 1 */

/* func.c */

int swap(int a, int b)
{
a = b;
return a;
}

/* main.c */

int b =1;
int c = 2;

int main(void)
{

swap(b,c);
printf("%d",c);
return 0;
}

BTW in my example of func.c and main.c here it doesn't seem to matter
if I make b, c global variables or local.
Because the parameter b would shadow out the global variable. This
is not a very Good Thing to do in general...

In writing C how do I best
decide if I should make variables global or local, or if it won't
affect the purpose of the code should I not care?
No, you should make them local unless there is a good reason to do
otherwise. (BTW, the terms the C Standard uses are "file scope"
and "block scope").
>
/* Example 2 */

/* func2.c */

int swap(int &a, int &b)
No such thing in C. You were thinking about C++? If so, go to
comp.lang.c++.
{
a = b;
return a;
}

/* main2.c */

int *b =1;
This is a constraint violation, though on some compilers it does
compile to the equivalent of int *b = (int *)1. Simplifying a bit,
b contains the address of the memory location 1. It doesn't even
need to be correctly aligned to contain an int. This is definitely
*not* what you were trying to do.
int *c = 2;

int main(void)
{

swap(&b,&c);
&b is the address of the object b, which in turn contains the
address of memory location 1.
printf("%d",&c);
return 0;
}

/* Example 3 */

/* func3.c */

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

/* main3.c */

int *b =1;
int *c = 2;

int main(void)
{

swap(*b,*c);
*b means the int located at the address contained in b. And you
are likely not to be allowed to read from address 1. Ka-Boom!
printf("%d",&c);
return 0;
}
Try this:
int swap(int *a, int *b)
{
*a = *b;
return *a;
}
int main(void)
{
int b = 1;
int c = 2;
swap(&b, &c);
printf("%d %d\n", b, c);
return 0;
}
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 15 '07 #3
"li******@gmail.com" <li******@gmail.comwrites:
I've read that C allows two ways to pass information between
functions:

o Pass by Value
o Pass by Reference

I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
Yes and no.

C does not have pass-by-reference as a built-in language feature. All
function arguments are passed by value. That's the most important
thing to remember.

But it's easy to *emulate* pass-by-reference by passing pointers (the
pointers are passed by value of course) -- just as you can emulate
linked lists using structs and pointers.

C doesn't have pass-by-reference, but that doesn't mean your C program
can't use pass-by-reference (or at least the equivalent of
pass-by-reference).

[...]
/* Example 1 */

/* func.c */

int swap(int a, int b)
{
a = b;
return a;
}

/* main.c */

int b =1;
int c = 2;

int main(void)
{

swap(b,c);
printf("%d",c);
return 0;
}
As you've seen, this doesn't work. But your swap() function doesn't
even *try* to swap its two arguments; Flash Gordon already covered
that. Since you're calling printf, you *must* have
#include <stdio.h>
at the top of your program. If your compiler doesn't warn you about
this, crank up the warning level. And you should print a '\n' at the
end of your program's output.

If you want to test whether a swap function works, you need to print
*both* values.

As a matter of style, your variable names are poorly chosen. You use
'a' and 'b' as the parameter names for swap(), but you use 'b' and 'c'
as the names of the variables whose values you pass to it. That's
perfectly legal (names in different scopes don't have to be distinct),
but it's confusing.

[...]
/* Example 2 */

/* func2.c */

int swap(int &a, int &b)
[snip]

This appears to be C++, not C. (C++ has a feature called "references"
that C does not.) If you want to program in C, figure out how to make
your compiler act as a C compiler.
/* Example 3 */

/* func3.c */

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

/* main3.c */

int *b =1;
int *c = 2;

int main(void)
{

swap(*b,*c);
printf("%d",&c);
return 0;
}
[...]

There are a number of serious errors in this code. Either your
compiler isn't telling you about them (in that case, you need to turn
up its diagnostic level), it's giving you warning messages and you're
ignoring them.

For example,
int *b = 1;
attempts to assign an int value to an int* variable. This is illegal
(more precisely it's a "constraint violation", and any conforming
compiler is required to complain about it).

Learn how to invoke your compiler so that (a) it compiles C, not C++,
and (b) so that it complains about as many errors in your code as
possible -- and pay close attention to any messages it produces, even
if they're just warnings.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #4
li******@gmail.com wrote:
I've read that C allows two ways to pass information between
functions:

o Pass by Value
o Pass by Reference

I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?
It depends on how 'Pass by Reference' is interpreted.
In general computer science, passing a pointer to some data qualifies
as 'Pass by Reference'.
In the context of C++, and for those who want to distinguish between the
parameter passing methods of C and C++, the term 'Pass by Reference'
gets reserved for the situation that C++ reference types are used. The
mechanism of passing a pointer is then referred to as 'Pass by
Address'.

You could also argue that C only has 'Pass by Value', because if you are
passing a pointer to something, then it is the value of the pointer
that gets passed.
>
Also I read that, "C does not have run-time typing". What does this
mean and is it true?
You will have to ask whoever made that statement what they meant with
the term 'run-time typing'.
One main characteristic of C is that all types used in the program, as
well as the exact type for each variable, must be known to the
compiler.
>
C just cares that you pass the correct type of arguments (int, float,
char, ...) and doesn't care about the value or variable/data structure
name is that also correct?
Yes, that is correct.
>
I wrote 3 simple test programs for the different ways to pass
information in functions. Any critiques/improvements would be welcome:
/* Example 1 */

/* func.c */

int swap(int a, int b)
{
a = b;
return a;
}
Given this implementation, I think the name 'assign' would have been
better for this function (same goes for the other examples).

This function uses 'Pass by Value', and therefor any changes you make to
a and b will be discarded as soon as you leave the function.
>
/* main.c */

int b =1;
int c = 2;

int main(void)
{

swap(b,c);
printf("%d",c);
return 0;
}

BTW in my example of func.c and main.c here it doesn't seem to matter
if I make b, c global variables or local. In writing C how do I best
decide if I should make variables global or local, or if it won't
affect the purpose of the code should I not care?
The default should be to use local variables.
The advantage of local variables is that they are only visible within
the block where they are declared. This means that in different
(non-nested) blocks, you can re-use the same name for different
purposes.

Global variables should be avoided as much as possible. Use them only
when there is no reasonable alternative and even then you should prefer
to declare them as 'static', which will limit their visibility to the
current source file.
>
/* Example 2 */

/* func2.c */

int swap(int &a, int &b)
This is a syntax error in C.

<snip>
/* Example 3 */

/* func3.c */

int swap(int *a, int *b)
{
*a = *b;
return *a;
}
This function uses 'Pass by Address'.
Changes to a and b themselves will not be visible or preserved outside
the function, but changes to *a or *b are visible and preserved.
>
/* main3.c */

int *b =1;
int *c = 2;
These two lines should generate a loud complaint from the compiler.
For this example, it is best to declare b and c the same as in example 1
int b =1;
int c = 2;
>
int main(void)
{

swap(*b,*c);
This is the wrong syntax for calling the function.
You need to pass the address of b and c, so you call the function like
this:
swap(&b, &c);
printf("%d",&c);
This should just be:
printf("%d", c);

But note that the function swap() does not actually modify its second
argument, so you will see no change here.
return 0;
}
A more useful illustration of 'Pass by Value and 'Pass by Address' would
be this:

#include <stdio.h>

void swap_value(int lhs, int rhs)
{
int temp;

printf("Inside swap_value(in): lhs = %d, rhs = %d\n", lhs, rhs);
temp = lhs;
lhs = rhs;
rhs = temp;
printf("Inside swap_value(out): lhs = %d, rhs = %d\n", lhs, rhs);
}

void swap_address(int* lhs, int* rhs)
{
int temp;

printf("Inside swap_address(in): *lhs = %d, *rhs = %d\n", *lhs, *rhs);
temp = *lhs;
*lhs = *rhs;
*rhs = temp;
printf("Inside swap_address(out): *lhs = %d, *rhs = %d\n", *lhs,
*rhs);
}

int main()
{
int a;
int b;

a = 1;
b = 42;
printf("Before swap_value(a,b): a = %d, b = %d\n", a, b);
swap_value(a,b);
printf("After swap_value(a,b): a = %d, b = %d\n", a, b);

a = 1;
b = 42;
printf("Before swap_address(a,b): a = %d, b = %d\n", a, b);
swap_address(a,b);
printf("After swap_address(a,b): a = %d, b = %d\n", a, b);

return 0;
}
>
PS: I'm learning about closures in Lisp and they seem nifty, but I
read C doesn't have them. Or is this not quite true?
C does indeed not have closures.
>
Lisp 9000
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Sep 15 '07 #5
In article <17****************@ingen.ddns.info>,
Bart van Ingen Schenau <ba**@ingen.ddns.infowrote:
>You could also argue that C only has 'Pass by Value', because if you are
passing a pointer to something, then it is the value of the pointer
that gets passed.
I think this is the clearest way to describe it. After all, if you
assign to the (pointer) value in the called function, it doesn't
affect anything in the caller. You have to explicitly dereference
the passed it to get the effect of call-by-reference.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 15 '07 #6
Keith Thompson wrote:
"li******@gmail.com" <li******@gmail.comwrites:
>I've read that C allows two ways to pass information between
functions:

o Pass by Value
o Pass by Reference

I was talking to some C programmers and they told me there is no such
thing as pass by reference in C since you are just passing an address
(or a pointer value address I guess?). So I was wondering is this
correct?

Yes and no.

C does not have pass-by-reference as a built-in language feature. All
function arguments are passed by value. That's the most important
thing to remember.

But it's easy to *emulate* pass-by-reference by passing pointers (the
pointers are passed by value of course) -- just as you can emulate
linked lists using structs and pointers.

C doesn't have pass-by-reference, but that doesn't mean your C program
can't use pass-by-reference (or at least the equivalent of
pass-by-reference).

[much snippage]
Pass-by-reference is the devil's spawn. In C++ it has its own syntax and
can be 'seen' at least. 11 years ago, in a land far away, I was
sentenced to maintain someone else's program, written in dBASEIV.

Three main storage classes of memory variables are PUBLIC, PRIVATE and
LOCAL. I was already a C programmer and PUBLIC simply meant file-scope
to me. Likewise, LOCAL was auto within a function block. This PRIVATE
thing was new and different. It is also the default storage class.

The effect of PRIVATE is to have calls to subroutines carry PRIVATE
variables with it without cluttering up the argument list.

a = 2
b = 4
c = 8

d = foo()
? d

func foo()
return a + b + c
endfunc

In the xBASE languages, ? is the print command and executing the above
will produce 14 on the console.

But PRIVATE has also the reference aspect like this..

a = 2
foo(a)
? a

func foo(x)
x = 9
return

The parameter x in foo is a reference to variable a in the caller. The
'? a' line will print 9. I don't remember anymore how long it took me to
figure that one out but more than a few hours.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Sep 17 '07 #7

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

Similar topics

14
by: dumboo | last post by:
hi there i m little bit confused over the following problem, i have understood wt the following code is doing...but not able to get the actual...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object....
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
10
by: Robert | last post by:
Hello all, I am a C programmer learning C++. And I am confused with function Pass by Pointer * or Pass by Reference &. Function pass by...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it...
7
by: Raymond Lewallen | last post by:
Which would be the proper way or the reason for using any of the following or combinations of the following? These are the 3 ways I've figured I...
1
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of...
4
by: S. | last post by:
Hi all, I have the requirement that I must pass-by-reference to my function addStudent() and getAge() functions where my getAge() function is...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.