in Education by
#include void display(int *q,int,int); int main(){ int a[3][4]={ 2,3,4,5, 5,7,6,8, 9,0,1,6 }; display(a,3,4); return 0; } void display(int *q,int row,int col){ int i,j; for(i=0;i<row;i++){ for(j=0;j<col;j++){ printf("%d",*(q+i*col+j)); } printf("\n"); } printf("\n"); } why this code show warning in gcc that "passing argument 1 of 'display' from incompatible pointer type display(a,3,4)"?...runs successfully anyway but curious to know about error..if anyone could tell this i would be grateful.. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The rule of "array decay" means that whenever you use an array name a as part of an expression, it "decays" to a pointer to the first element. For a 1D array, this is pretty straight forward. An array int a [10] would decay into type int*. However, in case of two-dimensional arrays, the first element of the 2D array is a 1D array. In your case, the first element of int a[3][4] has array type int [4]. The array decay rule gives you a pointer to such an array, an array pointer, of type int (*)[4]. This type is not compatible with the type int* that your function expects. However, by sheer luck, it would appear the the array pointer and a plain int pointer have the same representation on your system, and they happen to hold the same address, so the code works. You shouldn't rely on this though, it is not well-defined behavior and there is no guarantee it will work. You should fix your program in the following way: #include void display (int row, int col, int arr[row][col]); int main() { int a[3][4]= { {2,3,4,5}, {5,7,6,8}, {9,0,1,6}, }; display(3, 4, a); return 0; } void display (int row, int col, int arr[row][col]) { for(int i=0; i<row; i++) { for(int j=0; j<col; j++) { printf("%d ", arr[i][j]); } printf("\n"); } printf("\n"); } Here the array type that is the function parameter will silently "get adjusted" by the compiler to a pointer to the first element, int(*)[4], which matches what's passed to the function from the caller.

Related questions

0 votes
    #include void display(int *q,int,int); int main(){ int a[3][4]={ 2,3,4,5, 5,7,6,8, 9,0,1,6 }; display(a,3,4); return 0; } void display(int *q,int row,int col){ int i,j; for(i=0;i...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    This is a question I asked on another forum which received some decent answers, but I wanted to see ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    URL is http://*.*.*.*/100/?id=1&version=1 params is {"cityId": "110000", "query": {" ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    I create a project with "dotnet new angular". When a breakpoint is set on a Typescript instruction, it ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I have have no error in my code. But my condition, finally don't work. const togglePeronsHandler = ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I am new to VSTS and I am trying to get a msbuild task to publish a SQL Server project using a ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    axios.js throws CONNRESET error (certificate not found in request) but request package works for same p12 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    Simran write a code print: (“hello friends”)but when she executed there comes an error what was the error and why did it occur Select the correct answer from above options...
asked Dec 23, 2021 in Education by JackTerrance
0 votes
    I'm trying to analyze the nature of porting and virtual machines, with the intent of minimizing the amount ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    Here's the scenario: After installing the ASP.NET application (using install shield) in IIS6, when I right ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    Here's the scenario: After installing the ASP.NET application (using install shield) in IIS6, when I right ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
...