passing

This is a discussion on passing within the C Programming forums, part of the General Programming Boards category; Code: typedef struct{ int ID; char name[50]; double hor_total; }group; group salesman[SIZE]; int nextid(); int nextid(int last, int resetId) { ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    passing

    Code:
    typedef struct{
            int ID;
            char name[50];
            double hor_total;
        }group;
        group salesman[SIZE];
    
    int nextid();
    
    
    int nextid(int last, int resetId)
    {
        if ( resetId == 0)    
        {
            static int last;
            return ++last;
        }
    
        else
        {
            static int last = 0;
            return ++last;
        }
    
        
    }
    int main()
    {
        FILE *readId;
        int a, b, c = 0, last; 
        double ver_total = 0, max = 0; 
        int id, lastId, resetId = 0;
    
    
        if ( (readId = fopen("nextid.txt","r")) == NULL)
            printf("Cannot open nextid.txt file");
        else
        {
            fscanf(readId,"%d",&lastId);
            nextid(lastId, resetId);
        }
            for(a=0; a<SIZE; a++)
            {
                salesman[a].ID = nextid();
                salesman[a].hor_total = 0;
            }
    }
    I cannot use salesman[a].ID = nextid(); anymore. So, how can I past last variable in nextid() to salesman[a].ID?
    Last edited by ulti-killer; 10-28-2012 at 08:34 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Eh, your nextid function really confused me because at a glance, it looked like you were incrementing the same variable named last regardless of the value of resetId, but actually the variable named last in the else branch is a different variable. Personally, I think it would be clearer to write:
    Code:
    int nextid(int currentId, int isReset)
    {
        static int lastId = 0;
        if (isReset)
        {
            last = currentId;
        }
        return ++lastId;
    }
    Quote Originally Posted by ulti-killer
    I cannot use salesman[a].ID = nextid(); anymore. So, how can I past last variable in nextid() to salesman[a].ID?
    That sounds trivial, e.g.,
    Code:
    salesman[a].ID = nextid(lastId, resetId);
    Actually, this is horrible: you have a variable named readId that is not an id: it is a file pointer. Then you have a variable named lastId that is an id. Then you have another variable named resetId that is not an id: it is a flag variable. Oh, and in your nextid function and redundantly in your main function, you have a variable named last that is an id variable. Seriously, if you want to name your id variables with a fooId pattern, well and good, but don't go about naming non-id variables in the fooId pattern too, and don't name id variables with some other pattern unless you have very good reason to do so.
    Last edited by laserlight; 10-28-2012 at 08:43 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    What is fooId pattern?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Quote Originally Posted by ulti-killer
    What is fooId pattern?
    Your naming pattern whereby you have a name followed by an "Id" suffix.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    I know that as "Smurf Naming Convention" from Coding Horror: New Programming Jargon
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-06-2011, 03:46 AM
  2. Replies: 6
    Last Post: 04-04-2010, 11:48 AM
  3. passing value???
    By Tozilla in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2003, 10:12 AM
  4. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 12:25 AM
  5. passing arrays and passing pointers
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 12:35 PM

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