A question about passing values to a function.

Discussion in 'C Programming' started by Chad, Dec 18, 2009.

  1. Chad

    Chad Guest

    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()?
    Chad, Dec 18, 2009
    #1
    1. Advertising

  2. Chad

    Ben Pfaff Guest

    Chad <> 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;}}}
    Ben Pfaff, Dec 18, 2009
    #2
    1. Advertising

  3. In article <>,
    Chad <> 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?
    Kenny McCormack, Dec 18, 2009
    #3
  4. Chad

    Chad Guest

    On Dec 18, 10:44 am, (Kenny McCormack)
    wrote:
    > In article <>,
    >
    >
    >
    >
    >
    > Chad  <> 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?
    Chad, Dec 18, 2009
    #4
  5. Chad

    Stefan Ram Guest

    Chad <> writes:
    >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

    */
    Stefan Ram, Dec 18, 2009
    #5
  6. Chad <> writes:
    > On Dec 18, 10:44 am, (Kenny McCormack)
    > wrote:
    >> In article
    >> <>,
    >> Chad  <> 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?


    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) <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"
    Keith Thompson, Dec 19, 2009
    #6
  7. Chad

    spinoza1111 Guest

    On Dec 19, 2:44 am, (Kenny McCormack)
    wrote:
    > In article <>,
    >
    >
    >
    >
    >
    > Chad  <> 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, Dec 22, 2009
    #7
  8. Chad

    spinoza1111 Guest

    On Dec 20, 1:21 am, "Malcolm McLean" <> wrote:
    > "Chad" <> 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).


    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.
    spinoza1111, Dec 22, 2009
    #8
  9. In article <>,
    spinoza1111 <> 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...)
    Kenny McCormack, Dec 22, 2009
    #9
  10. On Fri, 18 Dec 2009 22:35:38 -0800, Keith Thompson <>
    wrote:

    > Chad <> 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.
    David Thompson, Jan 7, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,003
    Evan Carew
    Jan 21, 2004
  2. Replies:
    0
    Views:
    394
  3. Chris Rebert
    Replies:
    1
    Views:
    670
    Bobby
    May 28, 2009
  4. kie
    Replies:
    2
    Views:
    284
    W d'Anjos
    Oct 2, 2003
  5. cirillo_curiosone

    Passing values from a function generated html.

    cirillo_curiosone, Jul 26, 2004, in forum: Javascript
    Replies:
    0
    Views:
    83
    cirillo_curiosone
    Jul 26, 2004
Loading...

Share This Page