0

I am trying to declare an array of pointers each of which points to int arrays of different sizes. Any ideas?

2
  • 3
    Uhm, int **myarray?... Commented Apr 6, 2012 at 3:34
  • 1
    Use std::array<std::vector<int>, 5>. Commented Apr 6, 2012 at 3:41

6 Answers 6

3

From your description it sounds like you are looking for a pointer to a pointer.

int **aofa;
aofa = malloc(sizeof(int*) * NUM_ARRAYS);
for (int i = 0 ; i != NUM_ARRAYS ; i++) {
    aofa[i] = malloc(sizeof(int) * getNumItemsInArray(i));
}
for (int i = 0 ; i != NUM_ARRAYS ; i++) {
    for (int j = 0 ; j != getNumItemsInArray(i) ; j++) {
        aofa[i][j] = i + j;
    }
}

NUM_ARRAYS arrays may have different number of elements, as determined by the value returned by the getNumItemsInArray(i) function.

Sign up to request clarification or add additional context in comments.

Comments

3
int* ar[2];
int ar1[] = {1,2, 3};
int ar2[] = {5, 6, 7, 8, 9, 10};
ar[0] = ar1;
ar[1] = ar2;
cout << ar[1][2];

Comments

2

C version, should help clarifying the different types of declarations:

#include <stdio.h>

int main()
{
/* let's make the arrays first                                             */
int array_A[3] = {1, 2, 3};
int array_B[3] = {4, 5, 6};
int array_C[3] = {7, 8, 9};

/* now let's declare some pointers to such arrays:                          */
int (*pA)[3] = &array_A;
int (*pB)[3] = &array_B;
int (*pC)[3] = &array_C;  /* notice the difference:                         */
/* int *pA[3] would be an array of 3 pointers to int because the [] operator*/
/* has a higher precedence than *(pointer) operator. so the statement would */
/* read: array_of_3 elements of type_pointer_to_int                         */
/* BUT, "int (*pA)[3]" is read: pointer_A (points to) type_array_of_3_ints! */

/* so now we need a different array to hold these pointers:                 */
/* this is called an_ARRAY_of_3_pointers to_type_array_of_3_ints            */
int (*ARRAY[3])[3] = {pA, pB, pC};

/* along with a a double pointer to type_array_of_3_ints:                   */
int (**PTR)[3] = ARRAY;

/* and check that PTR now points to the first element of ARRAY:             */
if (*PTR == pA) printf("PTR points to the first pointer from ARRAY \n");

PTR++;

if (*PTR == pB) printf("PTR points to the second pointer from ARRAY! YAY!\n");

   return 0;
}

> $ clang prog.c -Wall -Wextra -std=gnu89 "-ansi"  output:   
> PTR points to the first pointer from ARRAY 
> PTR points to the second pointer from ARRAY! YAY!

Comments

0

Check out the section "Pointers to Arrays of Objects" http://www.functionx.com/cpp/Lesson24.htm It might help you.

Comments

0

In C++ you can declare it like shown below.new operator can work like similar to malloc in C.

int** array = new int*[n];

Comments

0
#include <iostream>

using namespace std;

#define arraySize 3

const int arr1[] = {48,49,50};
const int arr2[] = {64,65,66};
const int arr3[] = {67,68,69};

typedef const int (*arrayByte);

arrayByte arrayPointer[arraySize] = {
    arr1,arr2,arr3
};

void printArr(const int arr[], int size){
    for(uint8_t x=0;x<size;x++){
        printf("value%d=%d \n",x,arr[x]);
    }
}
int main()
{
    printf("Print Array 0\n");
    printArr(arrayPointer[0],arraySize);
    printf("Print Array 1\n");
    printArr(arrayPointer[1],arraySize);
    printf("Print Array 2\n");
    printArr(arrayPointer[2],arraySize);

    return 0;
}

try this code: C++ online

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.