Thread: rfgrgf, stupid pointers

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    rfgrgf, stupid pointers

    yes, this is another pointer question

    ok, here's my code:

    Code:
    #include <iostream.h>
    #include <math.h>  //edit, forgot to add this :)
    
    void ModifyPointer(double* value, bool*ok)  // forgot the void, too
    {
    value=sin(45);
    ok=true;
    }
    
    void main()
    {
    double temp1;
    bool temp2;
    
    ModifyPointer(&temp1,&temp2);
    cout << temp1 << " is " << temp2 << endl;
    }
    now, theoretically, that would make it so that temp1 and temp2 are equal to sin of 45 and true (respectively). Thing is, when I try and compile this, it returns an error:

    error C2440: '=' : cannot convert from 'double' to 'double *'

    and that ONLY happens for the sin one, it doesn't happen for the bool one. Now, i've tried MANY things (from reinterprets, to just plain (double *), and none of the options i've tried so far have worked....)

    so i need to know why this isn't working, and how i can fix it.

    NOTE: the above isn't my actual code, it's just pseudo code in case you haven't noticed, but it should compile and generate the error i think

    thanks in advance.
    Last edited by jverkoey; 04-10-2003 at 05:50 PM.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    ok, i'm stupid, rgfgrfggrgf

    found out that because i was using this in a switch statement that for some reason it wouldn't work right.........

    so i just made them equal temp variables, and when i got out of the switch, just assigned the temp vars to the pointers, and now it works

    but now, i'm trying to assign them as such:

    *ok=temp;

    but when i assign the value, the program crashes (lol), now i'm really confused

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    screw it, i'm just going to make the variables global (as much as i don't want to) but C++ is really pi$$ing me off right now

    so if anyone figures this out, just reply here, but otherwise, i'm moving on

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Dear oh dear the error tells you what you are doing. maybe you should read the faq pointers article.
    You are assigning a double to a pointer to a double.

    value=sin(45);

    lets look at the types

    double* = double;

    see the problem?

    The faq pointer tut will fix it for you.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    After reading the tutorial on pointers note that there is another way to do this: using references. Next, use int main (void main?????). Oh yea, use the newer headers.
    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void ModifyPointer(double &value, bool &ok)
    {
       value = sin(45);
       ok = true;
    }
    
    int main()
    {
       double temp1 = 0;
       bool temp2 = false;
    
       ModifyPointer(temp1, temp2);
       cout << temp1 << " is " << temp2 << endl;
    
       return 0;
    }

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Originally posted by jverkoey
    but now, i'm trying to assign them as such:

    *ok=temp;

    but when i assign the value, the program crashes (lol), now i'm really confused
    Thats because the ok pointer is probably invalid as in you never set it to anything useful. A "crazy pointer."

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, 04:23 AM
  2. 2D Array of Pointers
    By Slavakion in forum C++ Programming
    Replies: 12
    Last Post: 03-31-2004, 05:05 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM