c - Using 3d array outside a function -


I have a function in which I create a 3D array and fill all the values. I have to pass a pointer at the function which will allocate the memory space of the 3D array in that function so that it can be used outside of that function. At present, I am doing something that does not seem to work, can someone guide me in the best possible resolution?

  int (* arr) [4]; Assign zero to DD (int (* AR) [4]) {int local [2] [3] [4]; // Maybe we should pass * local? MEMSet (local, 0, size (integer) * 2 * 3 * 4); // fill the local array with the number arr = local; } printf ("% d \ n", arr [1] [2] [3]);   

I know I wrote the awesome code above but I am learning :)

Assigning an array is not possible. You are also using the wrong type for the argument ( is not int (*) [5] which decays in a int [2] [3] [4] , Use int (*) [3] [4] as the logic type) Once you have the correct type, you can use memcpy () You can use:

  #include & lt; String.h & gt; # Include & lt; Stdio.h & gt; Int ARR [2] [3] [4]; Assign zero to DD (int (* AR) [3] [4]) {int local [2] [3] [4]; MemSet (local, 0, size (local)); // local goes here because it is not an indicator, but an array passing * will start the first element of the local only array, i. E. Fill the local array with its first 2D piece // number memcpy (arr, local, sizeof (local)); } Int main () {assign_3D (arr); Printf ("% d \ n", ARM [1] [2] [3]); }   

But you can also return a new allocated array with your function:

  #include & lt; String.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; TypeFact type [2] [3] [4]; ArrayType * create_3D () {arrayType * results = Molec (size (* result); // Here we need dereference because the result is an indicator and we want the memory for the array, not the pointer to the memeset (result, 0, size (* result)); (* Results) [1] [2] [3] = 7; // fill the local array with numbers return results; // It's easy now, right? } int main () {arrayType * array = create_3D (); Printf ("% d \ n", (array * [1] [2] [3]); Free (Array); // CleanUp}   

Edit:
You know that the size of the first dimension is not known before running the function. In that case, you have to use the malloc () approach, but in a slightly different way:

  #include & lt; Stddef.h & gt; #include & lt; String.h & gt; # Include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; Typefahf slice type [3] [4]; SliceType * create_3D (size_t * firstDimSize) {* firstDimSize = 2; size_t array size = * first-letter size * size (slice type); SliceType * Result = malloc (arraySize); Memeset (result, 0, array size); Results [1] [2] [3] = 7; // fill the local array with numbers return results; } Int main () {size_t firstDim; sliceType * array = create_3D (& amp; firstDim); printf ("% d \ n", array [1] [2] [3]); Free (Array); // CleanUp}    

Comments

Popular posts from this blog

Verilog Error: output or inout port "Q" must be connected to a structural net expression -

jasper reports - How to center align barcode using jasperreports and barcode4j -

c# - ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value -