Thread: Question about an Array of Varying Arrays

  1. #1
    Registered User
    Join Date
    Aug 2015
    Location
    Australia
    Posts
    5

    Post Question about an Array of Varying Arrays

    Hello folks, just a quick question regarding initialising an array to contain other arrays.

    The problem I'm encountering is that I want to fill different elements of a large array (called "holder") with a smaller array (called "arr") which changes within each iteration of a loop.

    Below kind of what I was thinking of doing;

    Code:
    int L = 31;
    int n = 5;
    int* arr;
    arr = (int*)malloc(L*sizeof(int));
    
    // Here I need to initialise "holder", but I'm not sure how
    
    for (i = 0; i < n; i++) {
       while (val <= 0) {
           arr = GenerateArray(L);
           val = CalculateVal(arr);
        }
        holder[something][something] = something
    }
    Filling "arr" and using it as a stand-alone array is fine, but the problems I'm coming across are that I don't know how to initialise "holder", and then I don't know how to properly put "arr" into a cell (or across many?) within it.

    Some extra information, not sure if it will help or not.
    - "arr" is of length L, a non-constant variable
    - L is the first dimension of "holder"
    - n is the number of iterations of the loop, and as such the second dimension of "holder"

    Any help and advice is appreciated, cheers

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    I am curious as to how L is supposed to change on each iteration (you did write "array of varying arrays"), or if you actually have a dynamic rectangular 2D array (in which case there may be a more efficient technique available). Anyway, assuming that you are dealing with jagged arrays, one might write along these lines:
    Code:
    int L = 31;
    int n = 5;
    
    int **holder = malloc(n * sizeof(*holder));
    
    if (holder) {
        int *arr;
    
        for (i = 0; i < n; i++) {
            arr = malloc(L * sizeof(*arr));
            if (arr) {
                while (val <= 0) {
                    arr = GenerateArray(L);
                    val = CalculateVal(arr);
                }
                holder[i] = arr;
            } else {
                /* handle failure to allocate memory */
            }
        }
    } else {
        /* handle failure to allocate memory */
    }
    Remember to free what you malloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with printing a varying array
    By monki000 in forum C++ Programming
    Replies: 10
    Last Post: 04-19-2010, 04:06 PM
  2. Question about math... rendering varying sized boxes
    By Raigne in forum Game Programming
    Replies: 5
    Last Post: 04-23-2008, 03:44 PM
  3. Functions with varying augments
    By IdioticCreation in forum C++ Programming
    Replies: 7
    Last Post: 11-24-2006, 04:40 PM
  4. How to printf for varying variable
    By Fabii23 in forum C Programming
    Replies: 2
    Last Post: 10-01-2006, 07:45 PM
  5. varying size array
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2002, 06:29 PM

Tags for this Thread