Thread: Problem with changing variables :pointers, functions

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Work in Progress..... Jaken Veina's Avatar
    Join Date
    Mar 2005
    Location
    Missouri. Go Imos Pizza!
    Posts
    256
    Another problem is that *a and *b are both pointers since a and b are double pointers. So, whatever you assign to *a and *b is the ADDRESS of the variable that will be accessed with **a and **b. Therefore, when you say "*a =" and "*b =", the compiler is expecting an address after the equal signs. However, c and d are intergers, not addresses. If what you want is for **a to be the exact same value as c and for **b to be the exact same value as d, you need to say
    Code:
    *a = &c;
    *b = &d;
    That uses the address operator (&) on c and d, which tells the compiler to use the address of c and d, not the actual values of c and d. Here's kinda how the compiler is reading both *a = c and *b = d.
    Code:
    (pointer) = (interger)
    That's where the error "assignment makes pointer from interger without a cast" is coming from. You're assigning an interger value to a pointer value without a cast.

    A cast is basically somthing that turns one value into another type of value. If I wanted to use casting to fix that error (which you should note that you do NOT want to do in your particular situation) I would do this.
    Code:
    *a = (int*)c;
    *b = (int*)d;
    In that code, (int*) is the cast in both lines. And by putting it in front of c and d you are "casting" those interger values as pointers to an interger.

    Again, in your situation you would NOT want to do this. Let's say that c was 20 and d was 30. If you used this code
    Code:
    *a = (int*)c;
    *b = (int*)d;
    that would mean that when you used **a, the compiler would use the value stored at the memory address 20, which, 99.99999% of the time, will be memory that has absolutely nothing to do with the program you're writing. If you changed this piece of memory, you could get all kinds of different errors with your computer. The same goes for using **b. That means you're accessing the value stored at the memory address 30, which is most likely being used by some other application or program running on your computer.
    Last edited by Jaken Veina; 04-02-2005 at 06:05 PM.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    awesome

    thanks very much for all the replies, I see where I made my problem, there was no reason to declare the row and col variables as pointer, the function can change them if they are declared as regular integers, I guess I was trying to make it more complicated that it really is

    BTW pointers are so cool

    also thank you for the warm welcome, I can tell already I will be using these boards a lot in the furture =)
    ~Adam
    Last edited by SyCo1234; 04-02-2005 at 06:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to Class Member Functions
    By Dark_Phoenix in forum C++ Programming
    Replies: 6
    Last Post: 09-02-2007, 02:21 PM
  2. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM