multiplying with pointers

This is a discussion on multiplying with pointers within the C Programming forums, part of the General Programming Boards category; How can I multiple with pointers initialized? My compiler keeps telling me its improper use of a pointer but im ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    202

    multiplying with pointers

    How can I multiple with pointers initialized?
    My compiler keeps telling me its improper use of a pointer but im not trying to use a pointer :-(
    I was alway taught to multiple
    Code:
    var = constant * constant or var1 = var2 * var3
    or any mix of those, but the compiler thinks that Im using pointers. How can I get around this?

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Could you post some of the code that is giving you problems? There isn't any reason for a compiler to interpret that as a pointer statement, so I'm stumped.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    Thanks for any assistence you can offer.
    Code:
     static int *xpoint, *ypoint, PointY, PointX;
     *ypoint=GetDlgItemInt(hdc, PointY, NULL, TRUE) ;
     *xpoint=GetDlgItemInt(hdc, PointX, NULL, TRUE) ;
     ypoint=ypoint * yspace ;
     xpoint=xpoint * xspace ;

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    placing the * operator in variable declarations makes them pointers so xpoint and ypoint are pointers.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    yeah , but I dont want them to be treated as pointer in the equation. I want there value to be multiplied. Is there anyway I can do this?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
     static int xpoint, ypoint, PointY, PointX;
     ypoint=GetDlgItemInt(hdc, PointY, NULL, TRUE) ;
     xpoint=GetDlgItemInt(hdc, PointX, NULL, TRUE) ;
     ypoint *= yspace ;
     xpoint *= xspace ;

  7. #7
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    well, you need to point them at something, at the moment xpoint and ypoint are not initialised(think thats the right word).
    Code:
    xpoint = &something;
    ypoint = &somethingelse;
    
    *ypoint=GetDlgItemInt(hdc, PointY, NULL, TRUE) ;
    *xpoint=GetDlgItemInt(hdc, PointX, NULL, TRUE) ;

    ypoint=ypoint * yspace ;
    xpoint=xpoint * xspace ;
    without using the * operator the above code is working on addresses not values. Try this
    Code:
    *ypoint = (*ypoint) * yspace ;
    *xpoint = (*xpoint) * xspace ;
    just used brackets for clarity.
    If yspace and xspace have been declared as pointers also you need to use the * operator with them as well
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    That worked but another problem...it stops the thread at the first pointer claiming an access violation. Any clue whats wrong?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C_Coder already answered your question. You must make your pointer point to something before you try and use it. (HINT: That's why they're called "pointers", because you make them "point at" something!)

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

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    hmm

    Originally posted by Isometric
    Thanks for any assistence you can offer.
    Code:
     static int *xpoint, *ypoint, PointY, PointX;
     *ypoint=GetDlgItemInt(hdc, PointY, NULL, TRUE) ;
     *xpoint=GetDlgItemInt(hdc, PointX, NULL, TRUE) ;
     ypoint=ypoint * yspace ;
     xpoint=xpoint * xspace ;
    You are not multiplying the data, you are multiplying the adresses. Very dangerous...

    It should be *ypoint = *ypoint * yspace
    or ypoint[0] = ypoint[0] * yspace
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    I did that.
    That was suggested higher in the thread twice.

  12. #12
    Sayeh
    Guest
    All right, gather around... back to compiler and c-programming 101....


    A "pointer" is nothing more than a long value. No more. No less.

    It just so happens that the compiler expects to treat it with indirection because you have declared your variable as directable with the '*' symbol.

    At the very least, if your compiler can't handle pointer multiplication, then coerce each to a long in the multiply.

    Otherwise, be very careful of the resultant number because it could easily access a point well beyond that of your physcal and/or virtual RAM. Or it could roll and go negative.... the possibilities are endless!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 06:59 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21