*********************** ARRAYS ******************************
***********************************************************************
1] What would the output of the following program?
main()
{
char a[] = "Visual C++";
char *b = "Visual C++";
printf("\n %d %d",sizeof(a),sizeof(b));
printf("\n %d %d",sizeof(*a),sizeof(*b));
}
2] For the following statements would arr[3] and ptr[3] fetch the same character?
char arr[] = "Surprised";
char *ptr = "surprised";
3] For the statements in 9.2 does the compiler fetch the character arr[3] and ptr[3] in the same manner?
4] What would be the output of the following program, if the array begins at address 1200?
main()
{
int arr[] = {2,3,4,1,6};
printf("%d %d",arr, sizeof(arr));
}
5] Does mentioning the array name gives the base address in all the contexts?
6] What would be the output of the following prograam, if the aray begins at address 65486 ?
main()
{
int arr[] = {12,14,15,23,45};
printf("%u %u",arr, &arr);
}
7] Are the expressions arr and &arr same for an array of 10 integers ?
8] What would be the output of the following prograam, if the aray begins at address 65486 ?
main()
{
int arr[] = {12,14,15,23,45};
printf("%u %u",arr + 1, &arr + 1);
}
9] When are 'char a[]' and 'char *a' treated as same by the compiler ?
10] Would the following program compile successfully ?
main()
{
char a[] = "Sunstroke";
char *p = "Coldwave";
a = "Coldwave";
b = "Sunstroke";
printf("\n %s %s",a,p);
}
11] What would be the output of the following program ?
main()
{
float a[] = {12.4,2.3,4.5,6.7};
printf("\n %d",sizeof(a) / sizeof(a[0]));
}
12] A pointer to a block of memory is effectively same as an array.
13] What would be the output of the following program if the array begins at 65472?
main()
{
int a[3][4] = {
1,2,3,4,
4,3,2,1,
7,8,9,0
};
printf("\n %u %u",a + 1, &a + 1);
}
14] What does the follwoing declaration mean:
int(*ptr)[10];
15] If we pass the name of a 1-D int array to a function it decays into a pointer to an int. If we pass the name of a 2-D array of integers to a function what would it decay into ?
16] How would you define the function f() in the following program?
int arr[MAXROW][MAXCOL];
fun(arr);
17] What would be the output of the following program ?
main()
{
int a[3][4] = {
1,2,3,4,
4,3,2,8,
7,8,9,0
};
int *ptr;
ptr = &a[0][0];
fun(&ptr);
}
fun(int **p)
{
printf("\n %d",**p);
}
***********************************************************************
************************* ANSWERS ****************************
***********************************************************************
1] 11 2
1 1
2] Yes
3] No. For arr[3] the compiler generates code to start at location arr, move past it, and fetch the character there. When it sees the expression ptr[3] it generates the code to start at location stored in ptr, add three to the pointer, and finally fetch the character pointed to.
In other words, arr[3] is three places past the start of the object named arr, whereas ptr[3] is three places past the object pointed to by ptr.
4] 1200 10
5] No. Whenever mentioning the array name gives its base address it is said that the array has decayed into a pointer. This decaying doesn't take place in two situations:
--- When array name is used with sizeof operator.
--- When the array name is an operand of the & operator.
6] 65486 65486
7] No. Even though both may give the same addresses as in (6) they mean two different things. 'arr' gives the address of the first 'int' whereas '&arr' gives the address of array of 'ints'. Since these addresses happen to be same the results of the expressions are same.
8] 65488 65496
9] When using them as formal parameters while defining a function.
10] No, because we may assign a new string ot a pointer but not to an array.
11] 4
12] True
13] 65480 65496
14] 'ptr' is a pointer to an array of 10 integers.
15] It decays into a pointer to an array and not a pointer to a pointer.
16] fun(int a[][MAXCOL])
{
}
OR
fun(int (*ptr)[MAXCOL]) /* ptr is pointer to an array */
{
}
17] 1
No comments:
Post a Comment