A question about passing values to a function.

C

Chad

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()?
 
B

Ben Pfaff

Chad said:
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.
 
K

Kenny McCormack

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?
 
C

Chad

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?
 
S

Stefan Ram

Chad said:
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?

Data types are not run-time values in C.

But you can implement a run-time type system yourself:

#include <stdio.h>

#define INT 1
#define DOUBLE 2

union value { int int_; double double_; };

struct type { int type; union value value; };

void f( struct type const x )
{ printf( "%d\n", x.type );
switch( x.type )
{ case INT: printf( "%d\n", x.value.int_ ); break;
case DOUBLE: printf( "%g\n", x.value.double_ ); break; }}

int main( void )
{ struct type x;
x.type = INT; x.value.int_ = 5; f( x );
x.type = DOUBLE; x.value.double_ = 3.14; f( x ); }

/* prints:

1
5
2
3.14

*/
 
K

Keith Thompson

Chad said:
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.

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).
 
S

spinoza1111

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).

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.
 
K

Kenny McCormack

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?[/QUOTE]

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

(Sorry, couldn't resist...)
 
D

David Thompson

Chad <[email protected]> writes:

I'm fairly sure the answer is no, but it's not at all clear what you
mean.
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.
 

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,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top