passing pointers at structs thru functions

This is a discussion on passing pointers at structs thru functions within the C Programming forums, part of the General Programming Boards category; Somehow this won't work, and I cannot figure out why.. ! Code: Environment* f; f = fillEnvironmentF(e); Code: Environment* fillEnvironmentF(Environment* ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    6

    passing pointers at structs thru functions

    Somehow this won't work, and I cannot figure out why.. !

    Code:
    Environment* f;
    f = fillEnvironmentF(e);
    Code:
    Environment* fillEnvironmentF(Environment* environment){
    Environment fEnv;
    Environment* nE = &fEnv;
    
    [... here I fill up the struct fEnv, and that works]
    	
    return nE;
    }
    My problem is that after the function-call my variable f doesn't point at anything, it's empty. I know that nE contains all I want.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    fEnv is only valid for the duration of fillEnvironmentF().

    Don't return a pointer to a stack variable.

    gg

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    6
    thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing pointers outside of functions
    By thomas41546 in forum C Programming
    Replies: 6
    Last Post: 01-26-2008, 06:00 PM
  2. Passing pointers to two-dimensional arrays of structs
    By dr.neil.stewart in forum C Programming
    Replies: 2
    Last Post: 09-07-2007, 11:25 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Replies: 6
    Last Post: 05-06-2003, 04:08 PM
  5. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21