Thread: Very basic pointer problem

  1. #1
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59

    Very basic pointer problem

    Hi guys, I seem to have a very basic pointer problem.

    I have a main that has 2 counters, i need them to be updated by a function, so i decided to declare both int counters, and pointers to these int to pass to the function.

    This is how I (tried to) declare them:

    Code:
    int leftTriCont,rightTriCont;
    int* leftTriAbscissa,rightTriAbscissa;
    
    leftTriAbscissa=&leftTriCont;
        rightTriAbscissa=&rightTriCont;
    so leftTriAbscissa points to leftTriCont and the same with rightTriAbscissa and rightTriCont.

    So my idea was to pass the pointers to the function, BUT:

    I get an error, both on declaration, and when I call the famous function:

    main.c:56: warning: assignment makes integer from pointer without a cast

    main.c:160: warning: passing arg 4 of `fastTriangulate' makes pointer from integer without a cast

    Why that error? What did i do wrong in the declaration?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    "rightTriAbscissa" isn't a pointer, it should be:

    Code:
    int *leftTriAbscissa, *rightTriAbscissa;

  3. #3
    Registered User DeliriumCordia's Avatar
    Join Date
    Mar 2012
    Posts
    59
    Oh I tought it was only to put near the type... So it has to be near every var?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Yes, '*' needs to be with each variable. Although you will see some people argue the '*' should be next to the type. The compiler does not care where exactly the space is but putting it next to the type doesn't help.
    Why not just pass the addresses of the counters... no need to define new variables.
    Code:
    function(&leftTriCont, &rightTriCont);
    Inside the function you always reference the variables as *leftTriCont and *rightTriCont and any assignment you make will change their value outside the scope of the function.
    Last edited by nonoob; 09-10-2012 at 07:03 AM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by nonoob View Post
    Yes, '*' needs to be with each variable. Although you will see some people argue the '*' should be next to the type.
    Quite a few people also argue that only one variable at a time. So instead of
    Code:
    int *leftTriAbscissa, *rightTriAbscissa;
    they would do;
    Code:
        int *leftTriAbscissa;
        int *rightTriAbscissa;
    I personally prefer the asterix being near the variable name, as it indicates (say) that *leftTriAbscissa is an int or, equivalently, that leftTriAbscissa is a pointer. The location of whitespace does not matter.

    An alternative approach to the original problem is to use a typedef
    Code:
    typedef int *int_ptr;
    int_ptr  leftTriAbscissa, rightTriAbscissa;
    which defines both leftTriAbscissa and rightTriAbscissa as pointers.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    General format for declaration of pointer is

    <datatype> *(Variable_Name);

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by DeliriumCordia View Post
    Oh I tought it was only to put near the type... So it has to be near every var?
    Just to clarify, when you typed:
    Code:
    int* leftTriAbscissa,rightTriAbscissa;
    You made 2 variables, one of them was a pointer, the other was a regular int.

    So your original code was the same as:
    Code:
    int * leftTriAbscissa;
    int rightTriAbscissa;
    Check out my programming / algorithm blog @ http://www.swageroo.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic Pointer Help
    By Neti in forum C++ Programming
    Replies: 2
    Last Post: 07-28-2012, 02:02 PM
  2. very basic pointer questions--pls help
    By tkm in forum C Programming
    Replies: 7
    Last Post: 02-04-2011, 02:54 PM
  3. Basic pointer question
    By james123 in forum C Programming
    Replies: 4
    Last Post: 12-29-2009, 09:53 AM
  4. Basic pointer q
    By Jasper in forum C Programming
    Replies: 1
    Last Post: 11-28-2009, 02:44 AM
  5. Basic pointer question
    By MacNilly in forum C Programming
    Replies: 5
    Last Post: 06-19-2007, 08:31 AM