-1

I meet a case that I want to have an array of pointers(M rows, N cols), each member of this array points to a float vector (L‑length). Could you tell me how to establish it? I hope I can establish it dynamically because I usually don't know M, N, and L at first and L may be different for difference vector.

My occasion is that I need to read Green function with different distance(NDIS) and depth(NDP). So I need to create something like *grn[NDP][NDIS]. then use each pointer of this array to point to a component of green function.

By the way, I think it's a little bit complex than setting a size known array of pointers. Do you think it's worth to use this type of data structure? I'm trying to write a program to deal with observation, which I usually unknown it's size. However, it's ok for me to use a size-fixed array of pointers. If the total data is larger than the given size, I could ignore the over-sized part. But I'd hope to use them all.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
mlin
  • 11
  • 1
  • 1
    This will surely help you - [Stanford CS](http://cslibrary.stanford.edu/) – Ganesh Jun 19 '17 at 13:31
  • Maybe I didn't describe my question explicitly. I want to make a 2-d array of which each term is a POINTER pointers to a float vector. My case is that I need to read green function with different distance and depth. So I need to create something like *grn[NDP][NDIS]. – mlin Jun 19 '17 at 23:28

1 Answers1

0

You need to use the 2-d array and need to allocate it Dynamically, like this:

int i, j;
float **arr;
arr = malloc(sizeof(float*) * Size1);    //1st array setup
for(i = 0; i < Size1; i++)
  arr[i] = malloc(sizeof(float*) * Size2); // 2nd array setup at each element of 1st array

Now accessing it like this:

for(i = 0; i < Size1; i++)
  for(j = 0; j < Size2; j++)
    printf("%f", arr[i][j]);

Size1 & Size2 are sizes of first array and second array.

You can setup different sizes of individual arrays in first array elements, for that you need to do it without loop and by changing the Size2 variable. Hope this helps.

anil
  • 158
  • 1
  • 12
  • It is recommended practice that you should *not* cast the return value of `malloc()` as is done in this example – Toby Jun 19 '17 at 14:44
  • if you work with strict compilers(i mean strict checking rules like c++), they throw errors for this typecasting. So, this becomes a recommended practice in my knowledge. – anil Jun 19 '17 at 14:46
  • 1
    If you are using C++ then you are answering a C question incorrectly. casting of malloc return is always bad practice unless you are using a very old version of C (like K&R C). Even C89 correctly promotes void pointers meaning that no cast is required. See https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Toby Jun 19 '17 at 14:49
  • My answer is not limiting to C compilers only. Compilers and their behaviors can be changed, But our coding style must be robust enough to handle such warning and errors , so i think it is a recommended practice. Yes in C, it is automatically typecasted, but we cannot rely upon the Compiler behavior for such small things. – anil Jun 19 '17 at 15:04
  • 2
    The question is about C. If you are answering for another language then your answer is incorrect. If you like to cast the result of `malloc()` despite the evidence presented in the link in my last comment then that is up to you. However others would be wise to note that the overriding majority of C programmers would consider casting the result of malloc to be a big code smell. – Toby Jun 19 '17 at 15:09
  • 1
    "but we cannot rely upon the Compiler behavior for such small things"-- yes we can, in C. It is part of the language, not part of the compiler implementation. – ad absurdum Jun 19 '17 at 15:38
  • 1
    "*You need to use the 2-d array and need to allocate it Dynamically,*" Your code does not use a 2D-array, nor does it allocate one. The code you show in fact allocates 1+Size1 1D-arrays. The first, the single array is an array of pointers to `float` being able to Size1 elements, the other Size1 arrays are arrays of `float` being able to hold Size2 elements. This is called a "jagged", or "scattered" array, – alk Jun 19 '17 at 17:03
  • Sorry, I mean I need a 2-d array of pointers not a 2-d array. each item of this 2-d array is a float pointer to a float vector. – mlin Jun 19 '17 at 23:23