Assigning stderr to another stream

Discussion in 'C Programming' started by Fred, Mar 1, 2008.

  1. Fred

    Fred Guest

    I start with a program containing

    FILE *msgout;
    void msgset(FILE*, int);
    ....
    msgset(msgout, 47);

    In msgset I have (abbreviated)

    FILE *err_stream = NULL;
    void msgset(
    FILE *msgout,
    int iact
    )
    ....
    err_stream = stderr;
    msgout = err_stream; // Assignment here depends on iact.
    return;

    In the debugger msgout appears to get set correctly prior to the
    return.
    Upon return msgout is NULL.
    I'm hoping someone can educate me. Thanks,
    Fred
     
    Fred, Mar 1, 2008
    #1
    1. Advertising

  2. Fred

    Micah Cowan Guest

    Fred <> writes:

    > I start with a program containing
    >
    > FILE *msgout;
    > void msgset(FILE*, int);
    > ...
    > msgset(msgout, 47);
    >
    > In msgset I have (abbreviated)
    >
    > FILE *err_stream = NULL;
    > void msgset(
    > FILE *msgout,
    > int iact
    > )
    > ...
    > err_stream = stderr;
    > msgout = err_stream; // Assignment here depends on iact.
    > return;
    >
    > In the debugger msgout appears to get set correctly prior to the
    > return.
    > Upon return msgout is NULL.


    When you call msgset(), a _copy_ of the global msgout variable assigned
    to the parameter named msgout; and a _copy_ of the integral value 47
    is assigned to the aprameter named iact. You can assign new values to
    iact, just as you assigned a new value to msgout; but both of these
    variables are local to the body of msgset(), and will cease to exist
    when that function exits, and won't affect anything of the caller's.

    To change a variable of the callers, you need to pass in a pointer to
    that variable in the fuction call, and dereference it when you assign
    from within the function body. Yes, msgout is a pointer, but it's the
    FILE *, and not the FILE, that you are trying to change, so it's a
    FILE * for which you need to pass a pointer. In other words, what you
    need is a FILE **.

    ...
    msgset(&msgout, 47);
    ...
    void msgset(
    FILE **msgout,
    int iact
    )
    {
    ...
    *msgout = err_stream;
    ...
    }

    --
    HTH,
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
     
    Micah Cowan, Mar 1, 2008
    #2
    1. Advertising

  3. >FILE *msgout;
    >void msgset(FILE*, int);
    >...
    > msgset(msgout, 47);
    >
    >In msgset I have (abbreviated)
    >
    >FILE *err_stream = NULL;
    >void msgset(
    > FILE *msgout,
    > int iact
    > )
    >...
    >err_stream = stderr;
    >msgout = err_stream; // Assignment here depends on iact.
    >return;
    >
    >In the debugger msgout appears to get set correctly prior to the
    >return.


    You have two variables msgout, one of them is global, and the other
    is local to function msgset. Use of the same variable name like
    this is confusing.

    >Upon return msgout is NULL.


    C uses pass by value. You may assign to the function parameter
    until you are blue in the face, but it won't affect the global
    msgout. If you want to change a FILE * in a function, pass the
    address of it in (FILE **).

    If you wish to assign to the global variable directly, don't use a
    function parameter with the same name.
     
    Gordon Burditt, Mar 1, 2008
    #3
  4. Fred

    Fred Guest

    Many Thanks. Both of you cleared things up. I just needed to declare
    the argument as FILE** and pass in &msgout.
     
    Fred, Mar 1, 2008
    #4
  5. Fred

    Micah Cowan Guest

    Fred <> writes:

    > Many Thanks. Both of you cleared things up. I just needed to declare
    > the argument as FILE** and pass in &msgout.


    (And assign to *msgout, but I'm guessing you just forgot to mention
    that one.)

    I completely agree, though, that it's at least very confusing for you
    to have both a global and a local variable that share the same
    name. This tends to cause confusion about which variable is being
    referred to. I'd actually meant to draw attention to that in my first
    response, but forgot to.

    --
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
     
    Micah Cowan, Mar 1, 2008
    #5
    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. Dave Newberry
    Replies:
    0
    Views:
    673
    Dave Newberry
    Oct 17, 2004
  2. DOTNET
    Replies:
    3
    Views:
    540
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Sep 14, 2005
  3. Fuzzyman
    Replies:
    3
    Views:
    659
    Gabriel Genellina
    Jan 7, 2007
  4. Marcus Bristav
    Replies:
    1
    Views:
    160
    Chris Hulan
    Jan 30, 2008
  5. weston
    Replies:
    1
    Views:
    278
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page