Goeduhub:- AI, ML, HADOOP, Python and IOT Training provider.
Register for Free Certified Workshop on AI & Machine Learning :: 26-01-2020 || Career options for aspiring CS/IT, ECE, EE or EIC or EEE Engineers
0 like 0 dislike
4 views
in Tutorial & Interview questions by (6.7k points)

Passing multidimensional arrays to a function

1 Answer

0 like 0 dislike
by (6.7k points)
 
Best answer
Multidimensional arrays follow the same rules as single-dimensional arrays when passing them to a function. However the combination of decay-to-pointer, operator precedence, and the two different ways to declare a multidimensional array (array of arrays vs array of pointers) may make the declaration of such functions nonintuitive. The following example shows the correct ways to pass multidimensional arrays.

#include <assert.h>

#include <stdlib.h>

/* When passing a multidimensional array (i.e. an array of arrays) to a   function, it decays into a pointer to the first element as usual.  But only   the top level decays, so what is passed is a pointer to an array of some fixed   size (4 in this case). */

void f(int x[][4])

{    

assert(sizeof(*x) == sizeof(int) * 4);

}

/* This prototype is equivalent to f(int x[][4]).   The parentheses around *x are required because [index] has a higher   precedence than *expr, thus int *x[4] would normally be equivalent to int *(x[4]), i.e. an array of 4 pointers to int.  But if it's declared as a   function parameter, it decays into a pointer and becomes int **x,   which is not compatable with x[2][4]. */

void g(int (*x)[4])

{    

assert(sizeof(*x) == sizeof(int) * 4);

}

/* An array of pointers may be passed to this, since it'll decay into a pointer   to pointer, but an array of arrays may not. */

void h(int **x)

{    

assert(sizeof(*x) == sizeof(int*));

}

int main(void)

{    

int foo[2][4];    

f(foo);    

g(foo);

    /* Here we're dynamically creating an array of pointers.  Note that the       size of each dimension is not part of the datatype, and so the type       system just treats it as a pointer to pointer, not a pointer to array       or array of arrays. */    

int **bar = malloc(sizeof(*bar) * 2);    

assert(bar);    

for (size_t i = 0; i < 2; i++)

{        

bar[i] = malloc(sizeof(*bar[i]) * 4);        

assert(bar[i]);    

}

h(bar);       

for (size_t i = 0; i < 2; i++)

{        

free(bar[i]);    

}    

free(bar);

}

|| New Year Discount on summer training-2020 ||

20% OFF | on all summer training courses

|| Offer valid till 31-JAN-2020 (Extended Date) ||

Courses & Fee

  1. Augmented Reality & Virtual Reality - INR 15000/-
  2. AI, ML & Deep Learning - INR 12000/-
  3. Robotic Process Automation (RPA) - INR 12000/-
  4. Big Data HADOOP - INR 12,000/-
  5. Internet of Things (IoT) - INR 10,000/-
  6. Web development using python (Django) - INR 8000/-
  7. Industrial Automation (PLC-SCADA) - INR 8000/- 

Limited time offer !! Hurry up!!

[[ CALL - 07976731765 ]]

Register for Training

 Go to your Branch CSE or IT | ECE | EE, EIC or EEEMECE
 Know About Popular Colleges/Universities  List of IITsList of NITs | RTU-KOTA | Manipal University-Jaipur | JECRC University | Amity University Jaipur | BIT Mesra-Jaipur | MODY UNIVERSITY | LNMIIT-Jaipur | JK Lakshmipat | Banasthali Vidyapith | POORNIMA University
 Exams:   List of Exams After Graduation | List of Engineering Entrance Examinations (UG/PG) | JEE Main | JEE Advanced | GATE | IES | ISROList of PSUs
Placements:  List of companies | Logical Reasoning Questions | Quantitative Aptitude Questions | General English Questions | Technical-MCQ and Interview Questions
 Download Previous Year Papers For:  GATE | IES | RAJASTHAN TECHNICAL UNIVERSITY (RTU-Kota)RPSC Technical Exams | ISRO
 Online Free Training:  Artificial Intelligence(AI) & Machine Learning | Python Programming | Internet of Things-IoT | OpenCV (Open Source Computer Vision Library) | LINUX | Big Data : Hadoop | 
 Goeduhub
About Us | Contact Us   Social::   |  | 
...