Thread: reference transmision

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    reference transmision

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct
    {
            int grad;
            int a[20];
    } poli;
    
    void citire(poli *a)
    {
            int i;
            scanf("%d",&a->grad);
            for(i=a->grad-1;i>=0;i--)
                    scanf("%d",&a->a[i]);
    }
    
    void tipar(poli a)
    {
            int i;
            for(i=a.grad-1;i>=0;i--)
                    printf("%dX^%d ",a.a[i],i);
    }
    int main()
    {
            poli a;
            citire(&a);
            tipar(a);
            return 0;
    
    }
    why do i use & in the bolded lines ? wouldn't be more logical to read without & while variable a is a pointer ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    a is a pointer, but a->grad isn't a pointer (and that's what you're trying to read into).
    Since scanf needs a pointer to where it's going to store the answer, you need the &

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM