-1

I am a beginner into C programming so I have a basic question, in a lot of places in my code, I use some structures which have two-dim. pointer arrays as a variable, like in this example:

typedef struct example {

   char** elements;
   int number_elements;
   int x;
} example;

So how I did it usually is that I allocate memory for the structure and a number x for the variable elements,

  example* e = malloc(sizeof(example);
  if (e == NULL) exit(EXIT_FAILURE);

  e->elements = malloc(sizeof(char*) * x); 
  if (e->elements == NULL) //throw error

  for (int i=0; i<x; i++) {
     e->elements[i] = malloc(sizeof(char)*1024);
     if (e->elements[i] == NULL) //throw error
  }

So I am adding elements to the 2nd dimension dynamically. So if the 2.nd dimension gets full, I reallocate and update x.

So my question is, is there a nice or common way this number x can be hidden, because like this I store in the structure a variable for the number of elements in it and another variable for the actual number of elements allocated, and this is just ugly.

malajedala
  • 867
  • 2
  • 8
  • 22
  • You're "number x" for the variable elements seems redundant. You already *have* a member to store that: `int number_elements`, whose name is nothing if not self-explanatory. Are you trying to ask if there is a better way to manage *capacity* vs *size* (which are not always the same; capacity >= size, and once size reaches or is about to exceed capacity you then expand)? If so, then honestly this is the most straightforward way to do this (though I would choose a considerably more descriptive name than `x`). – WhozCraig Jul 27 '16 at 04:57
  • x was only for showing my example...i usually name my variables differently.. No I just asked basically if this is the way how "most" people do it, as I am just teaching myself C and have no clue... Are they maybe some good books for C where I can see and read such stuff? – malajedala Jul 27 '16 at 19:59
  • Puting `[c] books` in the search box of this site yields the following: [The Definitive C Book Guide and List](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). I concur with RSahu, however. Learn the basic language from some of those books, then work on data structures and modelling once you're more comfortable. – WhozCraig Jul 27 '16 at 20:57

1 Answers1

3

So my question is, is there a nice or common way this number x can be hidden, because like this I store in the structure a variable for the number of elements in it and another variable for the actual number of elements allocated, and this is just ugly.

It is not ugly. The value of x has to be stored somewhere. It's best to put it with the struct itself as a member since two instances of the struct can have different values of x. Looked at from that point of view, having x as a member of the struct is the most elegant solution, quite the opposite of what you think.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Ok seeing it from that point I guess it makes sense..I was just not sure if this is the way to do, so thanks. I have already asked in another comment, but do you maybe have some C books where I could read such stuff, so I don't bother asking these things? – malajedala Jul 27 '16 at 20:01
  • @malajedala, it's less a C language issue than a data modeling issue. You can search for books using "data modeling" – R Sahu Jul 27 '16 at 20:21