Array
-----------
Arrays
Predict the output of below program:
| #include <stdio.h> int main () {int arr[5]; Assume that base address of Arr is a and a size of integer//is a arr++; printf ("%u", arr); return 0; } |
| A |
2002 |
|
2004 |
| C |
By 2020 |
|
Lvalue Required |
discuss it
Question 1 Explanation:array name in C was implemented by a constant pointer. It is not possible to apply increment and decrement on constant types.
Predict the output of below program:
| #include <stdio.h> int main () {int arr[5]; Assume base address of Arr is in a and a size of integer is the + bit printf ("%u%u", arr + 1, &arr + 1); return 0; } |
|
2004 2020 |
| B |
2004 2004 |
|
2004 Garbage Value |
| D |
The program fails to compile because address-of operator cannot is used with array name |
discuss it
Question 2 Explanation:name of array in C gives the address (except in sizeof operator) of the first element. Adding 1 To this address gives the
address plus the sizeof typeThe array has. Applying the
address-ofOperator before the array name gives the address of the the whole array. Adding 1 To this address gives the
address plus the sizeof whole array.
What is output?
| # include <stdio.h> void print (int arr[]) {int n = sizeof (arr)/sizeof (arr[0]); int i; for (i = 0; i < n; i++) printf ("%d", Arr[i]);} int main () {int arr[] = {1, 2, 3, 4, 5, 6, 7, 8}; Print (arr); return 0; } |
| A |
1, 2, 3, 4, 5, 6, 7, 8 |
| B |
Compiler Error |
|
1 |
| D |
Run Time Error |
discuss it
Question 3 Explanation:see http://www.geeksforgeeks.org/using-sizof-operator-with-array-paratmeters/for Explanation.
Output of following program?
| #include <stdio.h> int main () {int a[] = {1, 2, 3, 4, 5, 6}; int *ptr = (int *) (&a+1); printf ("%d", * (ptr-1)); return 0; } |
| A |
1 |
| B |
2 |
|
6 |
| D |
Runtime Error |
discuss it
Question 4 explanation: &a is address of the whole array a[]. If we add 1 to &a, we get "base address of a[" + sizeof (a) ". The and this value are typecasted to int *. So ptr points the memory just after 6 are stored. PTR is typecasted to "int *" and value of * (PTR-1) is printed. Since ptr points memory after 6, ptr–1 points to 6.