Geeksquiz | Array

來源:互聯網
上載者:User

Array

-----------

Arrays

Question 1 WRONG
Predict the output of below program:
#include <stdio.h>   int main() {      int arr[5];            // Assume that base address of arr is 2000 and size of integer          // is 32 bit      arr++;      printf ( "%u" , arr);            return 0; }
A 2002
2004
C 2020
lvalue required

Discuss it


Question 1 Explanation:  Array name in C is implemented by a constant pointer. It is not possible to apply increment and decrement on constant types.
Question 2 WRONG
Predict the output of below program:
#include <stdio.h>   int main() {      int arr[5];      // Assume base address of arr is 2000 and size of integer is 32 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 be 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-of operator before the array name gives the address of the whole array. Adding 1 to this address gives the address plus the sizeof whole array.
Question 3 CORRECT
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.
Question 4 CORRECT
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)”. And this value is typecasted to int *. So ptr points the memory just after 6 is stored. ptr is typecasted to "int *" and value of *(ptr-1) is printed. Since ptr points memory after 6, ptr – 1 points to 6.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.