Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ questions?

int a[] = {1,2,3}; printf(“%d”, *(a+1));

what will be the output?

Update:

I'm a Java guy and I'm doing some interview questions that might come up in a future interview... thanks for answering...

4 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    The output will be 2.

    The trick is this: C/C++ compilers treat arrays and pointers the same ('cept of course when you allocate memory to a pointer using the malloc() or new functions you have to de-allocate it yourself with free() or delete()). Specifically, C -- and I know this better so bear with the older language's explanation -- will pass everything by value by default except arrays, which include strings in C. Pass by value of course means when it receives a parameter it creates a new variable of the same type and copies the value in the parameter it received into it. You can change it all you want, but unless you return it as the output of the function, or explicitly pass a pointer to the variable you want to change, it will have no effect on the original variable once the function ends. When the compiler sees an array being passed as a parameter, and this includes standard functions like printf, it passes the address of the first element, or array[0] which in this case has the value 1. Thus using the C address of operator (&, which is used to simplify pointer operations in C++) array=&array[0].

    Now we have established what a is in the printf parameter list, it's time to mention another feature of C/C++. Pointer arithmatic. When you add a number to a pointer you get the address of the next variable. It doesn't always check though, in this array, which is a[3] on a system where integers are 4 bytes a+1 is the address four bytes above a, a+2 is 8bytes a+4 is 16 bytes, and if it's told to print that out it will either print out garbage or go into a segmentation fault. We are responsible for seeing Pointer Arithmatic is kept within the bounds of the arrays we assign memory to.

    The equivalence of pointers and arrays is called "pointer decay". You can look that up and you will be able to understand it.

    Anyhow, the elements of int a[] are a[0], a[1], and a[2]. *a is the contents of a[0]. *(a+1) is the contents of a[1] which is 2.

  • 1 decade ago

    The variable "a" can be treated either as an array or as a pointer.

    When used in the expression a+1, the answer would be the RAM address of the beginning of the array, a, plus the size of one array element. The array elements are int, but the size of an int can vary in C++, so the answer would be

    a + sizeof(int)

    I don't remember how the printf("%d",...

    would convert this for output.

  • 1 decade ago

    int a[] is da declaration of array, here in printf ur pointing to the 1th location of an array,

    the intiilization of array is a[]={1,2,3}

    that means

    a[0]=1

    a[1]=2

    a[2]=3

    so in *(a+1) ur pointing to the location of array i.e 0+1 and is pointing to a[1] thus output iis 2

  • 1 decade ago

    would be some chars

    Source(s): do your own homework.com
Still have questions? Get your answers by asking now.