Thread: Doubt Related to Pointers usage in the function parameters?

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Doubt Related to Pointers usage in the function parameters?

    I have a two programs they consists of main and a function.

    In the first case.

    I am sending the output parameters to the function like below.

    In the function i am putting the values into the output parameters and then i am displaying

    them in the main.

    Here i need to access the output parameters with the '*' in function.


    Code:
    void getSumDifAndProduct(int a,int b,int *arr);
    
    void getSumDifAndProduct(int a,int b,int *arr)
    {
      *(arr+0) =  a+b;
      *(arr+1) =  (a-b);
      *(arr+2) = (a*b);
    }



    In the second case

    Iam using the structure variable and p

    Code:
    typedef struct
    {
      int sum;
      int diff;
      int product;
    
    }OutStruct_t;
    
    
    void getSumDifAndProduct(int a,int b,OutStruct_t *pSt);
    
    void getSumDifAndProduct(int a,int b,OutStruct_t *pSt)
    {
      pSt->sum =  a+b;
      pSt->diff =  (a-b);
      pSt->product = (a*b);
    }
    
    int main()
    {
      int a=0,b=0;
      OutStruct_t *pSt;
    
      printf("Please Enter the a and b values \n");
      scanf("%d %d",&a,&b);
    
      pSt = (OutStruct_t *) malloc(sizeof(OutStruct_t));
    
      getSumDifAndProduct(a,b,pSt);
    
      printf("a and b is %d %d \n",a,b);
      printf("a and b sum is %d \n",pSt->sum);
      printf("a and b dif is %d \n",pSt->diff);
      printf("a and b product is %d \n",pSt->product);
    
      return 0;
    }

    Here if i use *pSt->sum why does it gives me error like

    ex3.c:16: error: invalid type argument of `unary *'

    Iam bit confused about this variation.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, you may use (*pst).sum if you wish - but there's no benfit over pst->sum.

    I also prefer
    Code:
      arr[0] =  a+b;
      arr[1] =  (a-b);
      arr[2] = (a*b);
    instead of
    Code:
      *(arr+0) =  a+b;
      *(arr+1) =  (a-b);
      *(arr+2) = (a*b);

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because the -> operator alone 1) dereferences the pointer on the left hand to 2) access the struct member on the right hand. Why would you try to dereference pSt twice?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The a->b syntax essentially means (*a).b, so if you try to do *a->b, you get *(*a).b, and if a would be a pointer (not pointer to pointer), that would mean you first dereference and then try to dereference again, which can't be done.
    Note also that in C, you should never cast the return of malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147
    In the first case if iam using the individual variable like below

    Code:
    void getSumDifAndProduct(int a,int b,int *sum,int *dif,int *product);
    
    void getSumDifAndProduct(int a,int b,int *sum,int *dif,int *product)
    {
            *sum =  a+b;
            *dif =  (a-b);
            *product = (a*b);
    }
    
    int main()
    {
            int a=0,b=0,sum=0,dif=0,product=0;
    
            printf("Please Enter the a and b values \n");
            scanf("%d %d",&a,&b);
    
            getSumDifAndProduct(a,b,&sum,&dif,&product);
    
            printf("a and b is %d %d \n",a,b);
            printf("a and b sum is %d \n",sum);
            printf("a and b dif is %d \n",dif);
            printf("a and b product is %d \n",product);
    
            return 0;
    }
    i have to use the * before the variables .

    else

    sum = a+b;

    i get error like below



    ex1.c:7: warning: assignment makes pointer from integer without a cast


    I understand here as we need to put the value in the memory location of sum .

    But the structure variable should also be the same case .

    Why should we avoid giving the * here .

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have to tell the compiler that it is a memory deference (that you want it to follow the pointer), but there are different ways to do that. You can use a pointer and an array in almost the same way, do x[0] and *x and *(x + 0) are all the same thing, and you can choose, when it comes to structures, to either use the (*x).y or x->y - both are identical when it comes to what the computer actually does when it executes the code.

    Doing sum = a + b attempts to set the pointer itself to a + b, which will not achieve what you want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    India
    Posts
    147

    Thanks

    Thanks a lot to all of you for the detailed replies

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM