Thread: Pls help:: function initializing for 2d array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    22

    Pls help:: function initializing for 2d array

    I (dynamically) allocated memory for a 2D array. I realize that I must declare the 2nd dimension above the main function, but I do NOT know the value of this second dimension.
    ex.
    void rand_gen(int need_num, int array[][], int dim);

    Later on in the program, the user is asked to enter the dim (dimension) values.
    How do I correctly declare this function?

    this part of a big sliding numbers assignment (it is big for me!)
    i really appreciate any help. Thankx in advance!
    may help come to you when you need it

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    void rand_gen(int need_num, int **array, int dim);
    Or perhaps
    Code:
    void rand_gen(int need_num, int **array, size_t dim);
    [sizeof() returns a size_t, and malloc() takes a size_t.]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    Thanx for the help.
    but it gives me some errors
    error C2059: syntax error : '*'
    for that line

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by ypramesh
    I (dynamically) allocated memory for a 2D array. I realize that I must declare the 2nd dimension above the main function, but I do NOT know the value of this second dimension.
    ex.
    void rand_gen(int need_num, int array[][], int dim);

    Later on in the program, the user is asked to enter the dim (dimension) values.
    How do I correctly declare this function?

    this part of a big sliding numbers assignment (it is big for me!)
    i really appreciate any help. Thankx in advance!
    may help come to you when you need it
    If you know how many columes (arrays) you'll need at compile time, then you could use an array of pointers:

    char *arrptr[COLUMES];

    On the other hand, if you do not know how many arrays you'll need at compile time, than you can create the arrays dynamically at runtime via malloc, using a pointer to a pointer:
    Code:
     char** pp;
    pp = malloc(COLUMES * sizeof(*pp));
    
    for ( i = 0; i < COLMUES; ++i )
          pp[i] = malloc(ROWS/*elements per array*/  * sizeof(**pp));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM