C Arrays - GeeksforGeeks

02 Nov, 2020
Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n + m] be another integer array. 
C
Which of the following condition(s) hold(s) after the termination of the while loop? (GATE CS 2006) (i) j < m, k = n+j-1, and a[n-1] < b[j] if i = n (ii) i < n, k = m+i-1, and b[m-1] <= a[i] if j = m
only (i)
only (ii)
either (i) or (ii) but not both
neither (i) nor (ii)
The function xyz() is similar to merge() of mergeSort().Both the conditions (i) and (ii) are false in the sections k = n+j-1 and k = m+i-1 respectively. The while loop adds elements from a and b (whichever is smaller) to c and terminates when either of them exhausts. So, when the loop terminates either i=n or j=m. Suppose i=n. This would mean all elements from array a are added to c=>k must be incremented by n. c would also contain j elements from array b. So, the number of elements in c would be n+j and hence k=n+j. Similarly, when j=m, k=m+i. Hence, the option (D) is correct. (Had k started from −1 and not 0 and we used ++k inside the loop, the answer would have been option (C))
int main() 
{ 
  int *A[10], B[10][10]; 
  int C[] = {12, 11, 13, 14}; 
  
  /* No problem with below statement as A[2] is a pointer 
     and we are assigning a value to pointer */
  A[2] = C;  
  
  /* No problem with below statement also as array style indexing 
      can be done with pointers*/
  A[2][3] = 15; 
  
  /* Simple assignment to an element of a 2D array*/
  B[2][3]  = 15; 
  
  printf("%d %d", A[2][0], A[2][3]); 
  return 0;
}
Address of a[40][50] = Base address + 40*100*element_size + 50*element_size
                      = 0 + 4000*1 + 50*1
                     = 4050
Based on row major or column major
if row major then the result will be 4050
if column major then 
            Address of a[40][50] = Base address + 50*100*element_size + 40*element_size
                      = 0 + 5000*1 + 40*1
                     = 5040
Which of the following is true about arrays in C.  
For every type T, there can be an array of T. 
For every type T except void and function type, there can be an array of T. 
When an array is passed to a function, C compiler creates a copy of array. 
2D arrays are stored in column major form 
In C, we cannot have an array of void type and function types. For example, below program throws compiler error  
int main()
{
    void arr[100];
}
But we can have array of void pointers and function pointers. The below program works fine.  
int main()
{
    void *arr[100];
}
See examples of function pointers for details of array function pointers.  
     int arr[1000] = {0};  
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Comments
Old Comments
Must Do Coding Questions for Product Based Companies
Axios in React: A Guide for Beginners
How to set background images in ReactJS ?
Microsoft Interview Experience for Internship (Via Engage)
Difference between var, let and const keywords in JavaScript
Array of Objects in C++ with Examples
SQLAlchemy Core - Creating Table
How to Alter Multiple Columns at Once in SQL Server?
How to Replace Values in Column Based on Condition in Pandas?
How to Fix: SyntaxError: positional argument follows keyword argument in Python