Thread: Quick Question: C

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Quick Question: C

    okay, I was wondering how to get rid of global variables.

    For example, I have the following code:
    Code:
    int var1,var2,var3;  /* Global Vars, I want to get rid of */
    
    int main()  {
      newfunc();  
      printf("The result is %d\n",var3");
    }
    
    int newfunc()  {
      int result; 
      result=var1+var2;   /*  <-THIS LINE! ->     */
      var3=result;
      return (0);
    }
    Basically, I want to send a pointer or address of integer var3 and the values of var1 and var2 to the function newfunc() and then be able to make changes to var3 without having to return the value to main(). Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main ( void )
    {
        int loc1, loc2, loc3;
    
         loc1 = 1;
         loc2 = 2;
    
        loc3 = add1( loc1, loc2 );
        printf("Loc3 = %d\n", loc3 );
    
         loc1 = 2;
         loc2 = 4;
    
        add2( loc1, loc2, &loc3 );
        printf("Loc3 = %d\n", loc3 );
    
        return 0;
    }
    
    int add1( int one, int two )
    {
        return one + two;
    }
    
    void add2( int one, int two, int *three )
    {
        if( three != NULL )
        {
            *three = one + two;
        }
    }
    There are a few ways to do it. In the first example, we just assign the return value of the passed arguments to a variable. In the second, we pass a pointer to the value we want to change, so that changes to said variable in the funtion effect the varaible outside the function call as well.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    another question?

    Okay, so if I want to use a pointer (ex 2) would the prototype be the following or something different? and why?

    /* Possible Prototype ?? */

    void add2( int, int, int*)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: another question?

    Originally posted by justin69enoch
    Okay, so if I want to use a pointer (ex 2) would the prototype be the following or something different? and why?

    /* Possible Prototype ?? */

    void add2( int, int, int*)
    Yes, that'd work fine. Variable names in prototyping are optional. On that note, you can even call them one thing in one place, and different in another. Example:
    Code:
    void fun( int a, int b, int c );
    
    void fun( int abc, int efg, int hij )
    {
        ... stuff goes here...
    }
    Then you just refer to the variable name as it actually is in the declaration of the function. The protoype names are optional and they don't have to match up to what the actual declaration. The parameters have to match, but the names don't.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    53

    Talking Thank You

    I would like to thank you for all of your assistance. You have even been kind enough to give me the reasons behind it. This forum is great, and I cannot say that enough.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM