0

How do i create a three dimensional array where only one of the dimensions is known at compile time. The content of the array is struct values as

struct mat
{
char x[3];
int a;
}

struct samp
 {
   int a;
   struct mat;
 }

The array is supposed to store 'samp' and its size is as

   struct samp samp_arr[unknown][10][unknown];

the first time the program runs the first dimension of samp_arr will be one and the last dimension will grow with the number of samp structures put into the array. After a while the first dimension should be incremented by one and any undefined number of samp structs will be put into it. And so on

5
  • @CarlNorum: i just have the idea not the technical skills to do it. So I have not tried anything. Commented Feb 26, 2012 at 6:45
  • 1
    are you reinventing databases or something? if you don't have 'the technical skills', perhaps you need to read up before implementing 'ideas', as some other people, with technical skills, have already figured most of that stuff out. Commented Feb 26, 2012 at 6:56
  • @John are you willing to use GLib for your project? Commented Feb 26, 2012 at 6:58
  • @ivancho: is it a crime to ask something u don't know? I have a little bit of programming experience as a student and now i am trying to learn somethings from experienced friends like you. Commented Feb 26, 2012 at 7:05
  • 1
    @John the problem is that there are numerous resources other than the time of experts which you could have consulted first. We expect a good faith attempt on your part to learn before asking us. "i just have the ideas, not the skills" shows that you have not made such an attempt. Commented Feb 26, 2012 at 7:19

2 Answers 2

1

If you have a C99 complying compiler you don't have to re-invent the wheel, multi-dimensional arrays even with dynamic bounds are part of the language.

struct samp samp_arr[unknown][10][unknown];

(supposing that unknown is an expression that evaluates to the value of your liking.)

Usually it is a bad idea, though, to allocate such a large variable on the stack, so you should use malloc and friends to allocate it:

struct samp (*samp_arr)[10][unknown] = malloc(sizeof(struct samp[unknown][10][unknown]));
...
// use it
samp_arr[i][j][k].a = ...
...
free(samp_arr);

This declares a pointer to a two-dimensional array.

Wenn passing your array to functions you can do similar, you'd just have to watch that the array bounds come first in the argument list, such that they are known when it comes to the array itself:

int fun(size_t r, size_t s, size_t t, struct samp (*A)[s][t]) {
 ...
}
Sign up to request clarification or add additional context in comments.

Comments

1
#include <stdlib.h>
struct samp {
        int a;
};
int main(void)
{
    struct samp *(*sa)[10];
    int first_unknown = 2;
    int second_unknown = 4;
    int i,j,k;

    sa = malloc(sizeof(*sa) * first_unknown);
    for (i = 0; i < first_unknown; ++i) {
        for (j = 0; j < 10; ++j) {
            sa[i][j] = malloc(sizeof(*sa[i][j]) * second_unknown);
            for (k = 0; k < second_unknown; ++k)
                sa[i][j][k].a = 12345;
        }
    }
    return 0;
}

3 Comments

sorry for my naivety, but are not first_unknown and second_unknown fixed at compile time if we assign them values like that?
@John So you get the values from somewhere (stdin, file, whatever), from the appropriate I/O function instead of using hard-coded constants.
@John: while literal '2' and '4' are constants, 'first_unknown' and 'second_unknown' are not.

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.