Thread: Multiple return values form functions using Pointers

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Multiple return values form functions using Pointers

    Hi all,
    I need to ask this: I need to modify the value of some variables by calling another function.This has to do with elements of a linked list.

    for example:
    Code:
    funct1()
    {
      int a,b,c;
      struct yz *ptr;
      ptr=A[0];
      a=some expression;
      b= some expression;
      c=some expression;
      ptr=some modification;
    
      funct2(*ptr)
      a=some new expression;
      b= somenew expression;
    }
    The funct2 should take a value of the pointer which will be modified in my funct1. Now using this I need to change the values of a,b,c so that when I return back to funct1 I have new values which I then can use to evaluate my "some new expression" . Basically I want my functionto return multiple values. I cannot undestand how to use pointers for this.
    Some halp would be appreciated.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The following code uses pointers to change the values of integers passed to a function.

    Code:
    #include <stdio.h>
    
    void foo(int *i, int *j, int *k) 
    {
       *i = 1;
       *j = 2;
       *k = 3;
    }
    
    int main(void)
    {
       int a,b,c;
       foo(&a, &b, &c); /* pass the addresses of the integers */
       printf("a = %d, b = %d, c = %d\n",a,b,c);
       return 0;
    }

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    18

    Pointers in functions

    Hi Thanks for that but the problem is when I use this function I get a result which is different if I implement it without the function.
    I need to pass a pointer to a linked list too so my function would declaration would be:

    void func(struct yz *ptr, int *i, int *j, int *k);

    in my main i would call the function as

    func(newptr, &a, &b, &c);
    where newptr is defined before the function call..

    I suppose there is some problem withthe way I am passing the pointer.

    Could you suggest where I might be going wrong.
    Last edited by Nish; 03-11-2005 at 05:36 PM.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Post the code and the errors that always helps .
    Woop?

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    The part for which I need my function is indicated by start and end

    Code:
    for (k=0;k<40;k++)
        {
          xtemp = Y->Array[k];
         //start
          Nline = xtemp->line_used;
          Npin = xtemp->pin_no;
          xtemp = xtemp->nextptr;
    
          Wline = xtemp->line_used;
          Wpin = xtemp->pin_no;
          xtemp = xtemp->nextptr;
    
          Sline = xtemp->line_used;
          Spin = xtemp->pin_no;
          xtemp = xtemp->nextptr;
          
          Eline = xtemp->line_used;
          Epin = xtemp->pin_no;
          //end
        }
    So everytime I have a pointer of the same type which changes and hence I pass the pointer along with address of Nline, Npin etc.

    My function looks like this:

    Code:
    void get_line_pin(SWITCH *xtemp, int *t1, int *t2, int *t3, int *t4, int *t5, int *t6, int *t7, int *t8 )
    {
    
    
    
          *t1 = (xtemp->line_used);
          *t2 = (xtemp->pin_no);
          xtemp = xtemp->nextptr;
    
          *t3 = (xtemp->line_used);
          *t4 = (xtemp->pin_no);
          xtemp = xtemp->nextptr;
    
          *t5 = (xtemp->line_used);
          *t6 = (xtemp->pin_no);
          xtemp = xtemp->nextptr;
          
          *t7 = (xtemp->line_used);
          *t8 = (xtemp->pin_no);
    }

    and my function call is
    Code:
    get_line_pin(newptr, &Nline, &Npin..... uptil &Epin);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how is it possible to return multiple values?
    By Masterx in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2009, 06:49 PM
  2. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM