1

I have a 2D array like this and I want a pointer to it.

Currently I have this:

char* recv_args_msg_queue[20];
char** ref_temp = &recv_args_msg_queue[0];
char*** ref_queue = &ref_temp;

But I feel my way is really dumb. Is there a way to do it on one line?

Note:

char* recv_args_msg_queue[20]; is later in my code allocated properly to be an array. I just wanted dynamic allocation otherwise I could have wrote:

char recv_args_msg_queue[20][another_number_here];

TheCrafter
  • 1,909
  • 2
  • 23
  • 44
  • 3
    Where's the 2D array?!? – Antonio Apr 28 '15 at 08:56
  • `char* recv_args_msg_queue[20];` – TheCrafter Apr 28 '15 at 08:56
  • Do you properly allocate it? I mean, each of the 20 (dynamic) subarrays? – Antonio Apr 28 '15 at 08:57
  • 2
    Pointers are not arrays. Arrays are not pointers. War is peace. Slavery is freedom. Little-endian is big-endian. – Kerrek SB Apr 28 '15 at 08:57
  • @TheCrafter , that is a pointer to a pointer. **It is not a 2D array**. If you try to access it like a 2D array, you will get a Segmentation Fault. – Arun A S Apr 28 '15 at 08:58
  • @Antonio Yes later in my code. That should not be a problem with my question. – TheCrafter Apr 28 '15 at 08:58
  • 4
    `char* recv_args_msg_queue[20];` is not a 2D array -- it's an array of 20 `char` pointers. – Ulfalizer Apr 28 '15 at 08:59
  • `char* array_of_chars` is a pointer to a char. From there you can start a char array. Thus, `char** array_of_char_ptrs` is a pointer to a char*. From there you can start an array of char*. Am I wrong? – TheCrafter Apr 28 '15 at 09:01
  • 1
    @TheCrafter , "*`char* array_of_chars` is a pointer to a char. From there you can start a char array*". If you include `[` and `]`, it becomes "an array of pointer to `char`" – Spikatrix Apr 28 '15 at 09:02
  • In practice, don't do that. In C code, always prefer 1D arrays. – Basile Starynkevitch Apr 28 '15 at 09:03
  • @BasileStarynkevitch I want to implement something like a queue for strings. I did not see another way to do it. – TheCrafter Apr 28 '15 at 09:04
  • Use several `struct`, some of them containing a [flexible array member](http://en.wikipedia.org/wiki/Flexible_array_member) – Basile Starynkevitch Apr 28 '15 at 09:08
  • @BasileStarynkevitch I write my program with ANSI C Standard not C99. I should have stated that earlier. – TheCrafter Apr 28 '15 at 09:11
  • @TheCrafter: I believe that you are wrong in using such an obsolete standard for C. And if it is a requirement, you should have explicitly stated that (you need an ANSI C way, not a C99 way). Then you could use 0-sized array in `struct` instead of flexible array members. – Basile Starynkevitch Apr 28 '15 at 09:13
  • @BasileStarynkevitch It is not a commercial project. It is a personal one. I believe that one must learn how to walk before how to run. If I want to understand fully C99 and later C++, I feel I have to experience some ANSI C too. I will look into your 0-sized array. Thanks! – TheCrafter Apr 28 '15 at 09:26

1 Answers1

3

Using a typedef for your array type will make easier getting a pointer to it.

Your code would look like this:

typedef char* msg_queue20[20];

msg_queue20 recv_args_msg_queue;
msg_queue20* ref_queue = &recv_args_msg_queue;

Take care of reading the link I posted, as it contains important recommendations.

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197