Thread: pointer to an element of array as array?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    26

    pointer to an element of array as array?

    Is this possible? For my program, I have a lot of data that I want to store as one long array. This array will actually be a lot of smaller arrays "strung together". What I'd like to be able to do is essentially pass a subset of that array as an array to a function and treat it as an array. I.e. be able to access its members using the [] "square bracket operator thing".

    Can this work, does my question even make sense?

    Here is a code snippet that hopefully will make things clearer. This code doesn't actually work, it's what I want to work. I've tried various permutations of the code below but nothing I try is valid.

    Code:
        int myArray[10] = {1,2,3,4,5,6,7,8,9,10};
    
    
        int* sub_array = myArray[3];
        
        printf("In a perfect world this prints 5: %d", sub_array[1]);

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Think about this for a second. What is sub_array? An int pointer. What is myArray[3]? An integer.

    For that to print 5, do:

    Code:
    int* sub_array = myArray + 3;
    or

    Code:
    int* sub_array = &myArray[3];
    For an array of arrays, look up information about two dimensional arrays.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    Oh, thanks. I understand, that makes sense. I had forgotten to include the ampersand. I had tried several permutations then after seeing your code I looked back and realized my printf statement was printing a different variable. Silly me.

    I know about multi-dimensional arrays, I'm trying to represent a 3D array as a 1D array.

    Thanks for your help!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Dan_paul, there is a math equation to do that correctly. I've done it for 2D arrays, but not 3D.

    It's been posted on the forum, but I don't remember who posted it. It's been awhile back though. Worth Googling if you can't find it in the archive here.

    Once you get 2D to 1D working, you might just solve it yourself.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    26
    Thanks Adak. That will eventually come up I'm sure but for my purposes, the dimensions of the array aren't necessarily "spatial" (if that makes sense). But I was thinking about it and came up with a solution which may be old hat to others but seems really cool to me. The solution is to essentially have one long array of some base type then have progressively larger typedefs in which base types and other smaller typedefs are nested. Obviously, they will have to be even units (no overlapping).

    The practical use of this for me is that it allows me to store all my program data into one contiguous chunk of memory. I haven't gotten to this point yet, but I want to be able to easily copy and save the program data. It's also useful because it allows a fairly intuitive means of working with multi-dimensional arrays and nesting things deeply (though of course they're not really nested. Also, since they're typedefed, you can pass chunks of the data around to arrays and deal with them as autonomous pieces of data even though they're not. I'm still experimenting with this and trying to understand and figure everything out.

    Here's a simple example of what I'm talking about:

    Code:
    typedef int double_int[2];
    typedef int four_int[4];
    typedef int eight_int[8];
    
    
    int main()
    {
    
    
        int myArray[10] = {1,2,3,4,5,6,7,8,9,10};
    
    
        double_int* big = &myArray[0];
        four_int* bigger = &myArray[0];
        eight_int* biggest = &myArray[0];
    
    
        int* little_within_big = &bigger[2];
    
    
        printf("big: %d\n", *big[1]);//3
        printf("bigger: %d\n", *bigger[2]);//9
        printf("bigger: %d\n", *biggest[1]);//9
        printf("little_within_big: %d\n", little_within_big[1]);//10
    
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of an array poited by array element
    By mitofik in forum C Programming
    Replies: 7
    Last Post: 12-24-2010, 12:09 AM
  2. pointer to first element in array
    By someprogr in forum C Programming
    Replies: 4
    Last Post: 01-10-2009, 02:52 AM
  3. Replies: 19
    Last Post: 07-20-2007, 01:46 AM
  4. A pointer to multidim array element?
    By glo in forum C Programming
    Replies: 3
    Last Post: 06-23-2006, 04:00 PM
  5. pointer to the second element of 2-dim array
    By viaxd in forum C Programming
    Replies: 2
    Last Post: 10-23-2003, 01:34 PM