Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A question about passing values to a function.

0 views
Skip to first unread message

Chad

unread,
Dec 18, 2009, 1:38:55 PM12/18/09
to
Say I have the following...

void foo(int y)
{
/*do stuff*/
}

int main(void)
{
int x = 5;
foo(x);
return 0;
}

Does x, which has type int, get passed to foo() when I call foo()? Or
does the value 5 get passed to foo() when I call foo()?

Ben Pfaff

unread,
Dec 18, 2009, 1:44:54 PM12/18/09
to
Chad <cda...@gmail.com> writes:

> int main(void)
> {
> int x = 5;
> foo(x);
> return 0;
> }
>
> Does x, which has type int, get passed to foo() when I call foo()? Or
> does the value 5 get passed to foo() when I call foo()?

5.

To generalize a bit, if you write foo(x+1), then the value 6 gets
passed, not some kind of reference to the expression x+1.
--
char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
=b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}

Kenny McCormack

unread,
Dec 18, 2009, 1:44:56 PM12/18/09
to
In article <4b70facf-4a82-4ffc...@z3g2000prd.googlegroups.com>,

The answer is 5. But the question is: Why do you want to know?

Chad

unread,
Dec 18, 2009, 3:19:41 PM12/18/09
to
On Dec 18, 10:44 am, gaze...@shell.xmission.com (Kenny McCormack)
wrote:
> In article <4b70facf-4a82-4ffc-8637-bd7ef55de...@z3g2000prd.googlegroups.com>,

>
>
>
>
>
> Chad  <cdal...@gmail.com> wrote:
> >Say I have the following...
>
> >void foo(int y)
> >{
> >  /*do stuff*/
> >}
>
> >int main(void)
> >{
> >  int x = 5;
> >  foo(x);
> >  return 0;
> >}
>
> >Does x, which has type int, get passed to foo() when I call foo()? Or
> >does the value 5 get passed to foo() when I call foo()?
>
> The answer is 5.  But the question is: Why do you want to know?

I was wondering if it is possible to pass type int (as opposed to its
value) when I call foo(). Could I do this using pointers in C?

Message has been deleted

Keith Thompson

unread,
Dec 19, 2009, 1:35:38 AM12/19/09
to

I'm fairly sure the answer is no, but it's not at all clear what you
mean.

The type int doesn't have a value. A type can be thought of as, among
other things, a set of values. 5 is one of the values of type int,
and that's the value you're passing to foo; it's still a value of type
int when foo gets it, and it's stored in an object of type int (the
parameter object y).

Some languages support something called "templates" (as in C++) or
"generics" (as in Ada), and let you create an instance using a
particular type as an argument. C doesn't, but you can do something
similar with macros:

#define FOO(type) ...

...

FOO(int)

but that's just textual substitution. Even C++ templates and Ada
generics do the substitution at compile time.

Of course you can pass a pointer to an int rather than an int.

Some more exotic langauges might support someting like types as
values, but that's well beyond what can be done in C (at least
directly).

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Malcolm McLean

unread,
Dec 19, 2009, 12:21:09 PM12/19/09
to
"Chad" <cda...@gmail.com> wrote in message

>
>I was wondering if it is possible to pass type int (as opposed to its
>value) when I call foo(). Could I do this using pointers in C?
>
Not really. You can pass a void pointer followed by some sort of symbol to
tell the function foo() how to interpret the void *.

foo(void *ptr, char *type)
{
int xint;
float xfloat;

if(!strcmp(type, "int"))
xint = *(int *) ptr;
else if(!strcmp(type, "float"))
xfloat = *(float *) ptr;
}

However there's rarely much point doing this. An exception is the printf()
family of functions, which use a related mechanism. (The format string is
used to pull the other arguments off a variable-length argument stack).


spinoza1111

unread,
Dec 22, 2009, 7:12:53 AM12/22/09
to
On Dec 19, 2:44 am, gaze...@shell.xmission.com (Kenny McCormack)
> Chad  <cdal...@gmail.com> wrote:
> >Say I have the following...
>
> >void foo(int y)
> >{
> >  /*do stuff*/
> >}
>
> >int main(void)
> >{
> >  int x = 5;
> >  foo(x);
> >  return 0;
> >}
>
> >Does x, which has type int, get passed to foo() when I call foo()? Or
> >does the value 5 get passed to foo() when I call foo()?
>
> The answer is 5.  But the question is: Why do you want to know?

Why do you want to know why he wants to know?

spinoza1111

unread,
Dec 22, 2009, 7:20:00 AM12/22/09
to
On Dec 20, 1:21 am, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> "Chad" <cdal...@gmail.com> wrote in message

To a C program, types are metaphysical creatures,
Angels dancing on a head of a pin.
Call me anytime you want to call, baby
But call me by value don't call me by name.
Self-reflection is deliberately discouraged:
Considered onanistic by the managerial class.
Values must appear on the stack as by Magic
As God created the earth in seven days.
Actual thinking is deliberately discouraged:
Mindless coding is preferred.
The result of course was the massive sprintf boner
Which allows overwriting memory even in a program that works.
Thus control of man by man creates Control's opposite.

Kenny McCormack

unread,
Dec 22, 2009, 7:26:15 AM12/22/09
to
In article <7f079e95-98c5-45d4...@s21g2000prm.googlegroups.com>,
spinoza1111 <spino...@yahoo.com> wrote:
...

>> The answer is 5. �But the question is: Why do you want to know?
>
>Why do you want to know why he wants to know?

Why do you want to know why I want to know why he wants to know?

(Sorry, couldn't resist...)

David Thompson

unread,
Jan 7, 2010, 4:17:33 AM1/7/10
to
On Fri, 18 Dec 2009 22:35:38 -0800, Keith Thompson <ks...@mib.org>
wrote:

> Chad <cda...@gmail.com> writes:
<snip>


> > I was wondering if it is possible to pass type int (as opposed to its
> > value) when I call foo(). Could I do this using pointers in C?
>
> I'm fairly sure the answer is no, but it's not at all clear what you
> mean.

<snip>


> Some languages support something called "templates" (as in C++) or
> "generics" (as in Ada), and let you create an instance using a
> particular type as an argument. C doesn't, but you can do something
> similar with macros:
>
> #define FOO(type) ...
>
> ...
>
> FOO(int)
>
> but that's just textual substitution. Even C++ templates and Ada
> generics do the substitution at compile time.
>

Not quite. C++ templates were implemented (before being standardized)
as instantiation, and (now) take advantage of the range that allows.

Ada, in this and other areas, was designed before implementation to
allow different implementations, and in particular generics are
constrained (just) enough that they *can* be implemented as
precompiled/shared code using dope vectors, thunks, etc.
GNAT-now-again-part-of-GCC does choose instantiation.

> Of course you can pass a pointer to an int rather than an int.
>

to be exact, a pointer to an int object aka variable; or even more so,
a value of type pointer to int that points to an object of type int.
And then use the pointer (value) to access the object.
Which is the typical and useful case.

0 new messages