Q5.Problem

Q.5. output 10 is clear but why is there 0 in it ?

1
2
3
4
5
6
7
8
9
10
  const int ARRAY_LEN=100;
int main()
{
int arr[ARRAY_LEN] = {10}; // Note implicit initialization of
 // other elements
 int *xPtr = arr, *yPtr = arr + ARRAY_LEN -2;
 std::cout << *xPtr << " " << *yPtr; //should show 10 10 but why does it show 10 0
 return 0;
}
If you initialize an array with the {} syntax, extra elements are initialized to 0.
in line 6 i dont understand if *xptr is intialized to arr shouln't there be semicolon for the statement to be complete instead there is a comma and further *yPtr is intialized to arr + ARRAY_LEN-2 which makes nosense to me i think i am misunderstanding the concept PLUS the link shows the basics of array intialization and stuff to which i am familiar to
 
int *xPtr = arr, *yPtr = arr + ARRAY_LEN -2;
i dont understand if *xptr is intialized to arr shouln't there be semicolon for the statement to be complete instead there is a comma


remember, comma can be used to declare multiple variables of a same type :
1
2
3
4
int x, y; 
// is the same as :
int x;
int y;

but not different types :
 
int x, char y; // error, you must use semicolon instead 


the only difference in your code is the variables declared are pointer to int :
1
2
3
4
int *xPtr = arr, *yPtr = arr + ARRAY_LEN - 2;
// is the same as
int *xPtr = arr;
int *yPtr = arr + ARRAY_LEN - 2;


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

other comment removed due to wrong info ://
Last edited on
Thank you the you made the declaration part clear now just one thing arr has a value of 10 as cout<<*xPtr is 10 shouldnt than *yPtr give 10 + 0 which should give full output as 10 10 why does it show 10 0
1
2
3
4
5
  const int ARRAY_LEN=5;

int arr[ARRAY_LEN] = { 10 };
// is the same as
int arr[ARRAY_LEN] = { 10, 0, 0, 0, 0 };
Thank you :)
Topic archived. No new replies allowed.