Problem Passing Values between functions

This is a discussion on Problem Passing Values between functions within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm using visual studio 2005 on windows. I'm creating a struct that I'm trying to pass between functions. I ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    80

    Problem Passing Values between functions

    Hi,

    I'm using visual studio 2005 on windows.

    I'm creating a struct that I'm trying to pass between
    functions. I set it up in a header file:

    Code:
    typedef struct DataElement2 {
        char Sub[75];
        int Pos;
    } Substituent;
    I initialize it in my main function :

    Code:
    SP = new DataElement2 [ 75 ];
    I want to pass this to another function where it gets populated.  The function's name is ev_num.  This function
    is called multiple times. Leaving other parts out for
    simplicity:
    main function call:

    Code:
    loop {
    ev_num(SP);
    }
    ev_num:

    Code:
    int ev_num(Substituent *SP) {
     // figured out SubPosCounter in some code above (leaving out - know its correct)
     SP[SubPosCounter].Pos = t->p[branchid];
     SubPosCounter++;
    }
    When I print out SP at the end of my main function, its
    missing values that were found in the individual calls. Not
    sure what I'm doing wrong, seems like SP gets erased each
    time I call ev_num.

    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    It looks like your SubPosCounter gets reset everytime you call ev_num. To fix this either make SubPosCounter static or pass SubPosCounter as an argument to ev_num(). The second approach is probably better in this case because that way you could call ev_num() later in the code with a reset SubPosCounter counter.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    1. Check your subscript is in range?

    2. Maybe do
    ev_num( &SP[ SubPosCounter++] );
    so that the function being called has less dependency on the outside world.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structs to callback functions (GTK+ related)
    By Raskalnikov in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 12:46 PM
  2. Problem with passing structs to a function.
    By darsh1120 in forum C Programming
    Replies: 7
    Last Post: 03-11-2008, 04:36 AM
  3. Problem with values changing
    By patra_user in forum C++ Programming
    Replies: 19
    Last Post: 12-12-2007, 01:12 PM
  4. passing functions with variable
    By itld in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2001, 10:43 PM
  5. Passing values from function to function
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2001, 07:28 AM

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