Geeksquiz | Array

Source: Internet
Author: 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 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.
Question 2 wrong
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.
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) ". 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.