Thread: (Trivial) pointer question

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    115

    (Trivial) pointer question

    Hello

    I have very little programming experience with C++. I have something like this:

    Code:
    bool MYFUNC( const unsigned *N, double *prob, double *corr )
    {
    bool flag = false;
    printf( "%u ; %f ; %f\n" , *N , *prob , *corr );
    
    double ma[ *N ] = {0.0}; // ERROR
    
    // other things
    
    return( flag );
    }
    
    int main()
    {
    const unsigned n = 9;
    static double p = 0.5;
    static double c = 0.1;
    
    MYFUNC ( &n , &p , &c );
    }
    I want to pass three pointers to MYFUNC and in it initialize a array of size N called "ma". This produces an error. Why? Actially I want to initialize an array of size (N+1).

    Thanks for you help
    Serge

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    When you declare an automatic array in C++, the size of it must be a compile-time constant (OTOH, in C99 variable-length arrays are legal).

    So you'd either use a fixed-size array which you hope is large enough, or allocate it dynamically (use std::vector).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I urge you not to use printf. Use std::cout and I/O manipulators. You can also use Boost.Format (although that documentation is ........ty, to say the least).
    Also prefer to pass by reference instead of by address.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. A pointer question.
    By joenching in forum C++ Programming
    Replies: 7
    Last Post: 03-20-2008, 04:10 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM