0

I am trying to make an array which contains 1D arrays of varying length. Due to the variation in length, I cannot use a 2D array. My code is as follows:

int ROW = 6;
int COL = 4;
int faceverts[6][4] = {{3,2,1,0}, {4,5,1,0}, {2,6,5,1}, {2,3,5,6}, {7,3,0,4}, {1,6,7,4}};   
int (*q)[4] = faceverts;

int main(){
    for (int i = 0; i < ROW; i++){
        for (int k = 0; k < COL; k++)
            printf("%d ", *(*(q+i)+k));
        printf("\n");
    }
}

My goal is to be able to get rid of those ROW and COL variables, as well as not have a fixed 2D array, but rather an array of 1D arrays of varying length. I have been told that using pointers is key to doing this task, but I do not know how to do this myself.

17
  • you will need to use malloc then, and instead of int faceverts[6][4], make it int **faceverts Commented Jun 20, 2018 at 21:52
  • Is this straight up C, no C++? Commented Jun 20, 2018 at 21:52
  • 1
    What you want is an array of pointers. Those pointers will then themselves point to your arrays of varying size. Commented Jun 20, 2018 at 21:56
  • 1
    Is it possible I can use pointers to multiple 1D arrays of varying length? . Arrays cannot "vary" in length once allocated either on the heap or on the stack. You can move pointers around / reallocate. But concept of "varying" an array length is not a thing. Commented Jun 20, 2018 at 21:57
  • 1
    Initialization in C is done with fixed sizes determined when the code is compiled. If you need variable sizes determined at runtime, you cannot use initialization — you have to use run-time assignments in some shape or form. Commented Jun 20, 2018 at 21:59

2 Answers 2

1

One approach is to create an array of structures. Each structure element contains a pointer and count to an array.

#include <stdio.h>
#include <stdlib.h>

typedef struct {
  int *a;
  size_t n;
} TwoD;

#define AN(a) (sizeof (a)/sizeof (a)[0])

int smallest[1] = { 1 };
int smaller[2] = { 2, 3 };
int small[3] = { 4, 5, 6 };
int big[4] = { 7, 8, 9, 10 };
int bigger[5] = { 11, 12, 13, 14, 15 };
int biggest[6] = { 16, 17, 18, 19, 20, 21 };

TwoD faceverts[6] = {
    { smallest, AN(smallest) }, { smaller, AN(smaller) }, { small, AN(small) }, 
    { big, AN(big) }, { bigger, AN(bigger) }, { biggest, AN(biggest) } };

int main(void) {
  // Let us change things a bit at run time
  int change[] = { 22,23,24,25,26,27,28 };
  faceverts[0] = (TwoD) {change, AN(change)};  // compound literal
  for (size_t row = 0; row < AN(faceverts); row++) {
    for (size_t col = 0; col < faceverts[row].n; col++) {
      printf(" %d", faceverts[row].a[col]);
    }
    printf("\n");
  }
  return 0;
}

Output

 22 23 24 25 26 27 28
 2 3
 4 5 6
 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20 21
Sign up to request clarification or add additional context in comments.

Comments

0

There is only 1D array in memory. (2D array is actually a long 1D array)

Allocate a 2D array (rows, cols)

int *arr;
int size;
size = width * height;
arr = (int *) malloc(sizeof(int) * size);

Access element (x, y) in the array

int e;
int offset;
offset = x * cols + y;
e = *(arr + offset);

Free the array

free(arr);

Allocate a irregular 2D array (col_0, col_2, ...., col_n-1);

int arr;
int size = 0;
// size = col_0 + ... + col_n-1;
arr = (int *) malloc(sizeof(int) * size);

Access element (x_row, y_col) in the array

int e;
int offset;
// offset = col_1 + ... + col_x-1 + y_col;
e = *(arr + offset);

Comments

Your Answer

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