non-const argument and const pointer

This is a discussion on non-const argument and const pointer within the C++ Programming forums, part of the General Programming Boards category; Hello all, I have a C function Code: void foo (double * x, size_t s); where x is a vector ...

  1. #1
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50

    non-const argument and const pointer

    Hello all,

    I have a C function
    Code:
    void foo (double * x, size_t s);
    where x is a vector and which I know that doesn't modify x.

    And I have a pointer
    Code:
    const double * px = A.get_ptr ();
    Is there a way to pass px to foo, without using const_cast, i.e. other than
    Code:
    double * qx = const_cast < double * > (px);
    ?

    Thanks

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    >> I have a C function ... which I know that doesn't modify x.
    This is one the reasons const_casts<> exists.

    If you must, you could create a full copy of the double and use that:
    Code:
    double x = *A.get_ptr();
    double *qx = &x;
    gg

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by nepper271 View Post
    Hello all,

    I have a C function
    Code:
    void foo (double * x, size_t s);
    where x is a vector and which I know that doesn't modify x.

    And I have a pointer
    Code:
    const double * px = A.get_ptr ();
    Is there a way to pass px to foo, without using const_cast, i.e. other than
    Code:
    double * qx = const_cast < double * > (px);
    ?

    Thanks
    why don't you use constant parameter if you know x won't be modified?
    Code:
    void foo( const double * x, size_t s);
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User nepper271's Avatar
    Join Date
    Jan 2008
    Location
    Brazil
    Posts
    50
    Quote Originally Posted by nimitzhunter View Post
    why don't you use constant parameter if you know x won't be modified?
    Code:
    void foo( const double * x, size_t s);
    foo is not my function, is part of a C library.

  5. #5
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    452
    Quote Originally Posted by nepper271 View Post
    foo is not my function, is part of a C library.
    Oh. my bad.
    "All that we see or seem
    Is but a dream within a dream." - Poe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. compile-time vs. runtime const multiplication
    By CodeMonkey in forum C++ Programming
    Replies: 5
    Last Post: 08-21-2010, 05:32 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. const qualifier used on a function argument
    By hzmonte in forum C Programming
    Replies: 27
    Last Post: 04-18-2006, 11:08 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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