Two Plus Two Publishing LLC Two Plus Two Publishing LLC
 

Go Back   Two Plus Two Poker Forums > Other Topics > Programming

Notices

Programming Discussions about computer programming

Reply
 
Thread Tools Display Modes
Old 07-24-2012, 02:52 PM   #1
veteran
 
Drag007's Avatar
 
Join Date: Jul 2005
Posts: 2,014
Simple C++ question

Learning c++ on my own - why would printing *yPtr output 0?

1 #include <iostream >
2 const int ARRAY_LEN = 10;
3
4 int main() {
5 int arr[ARRAY_LEN] = {10}; // Note implicit initialization of
6 // other elements
7 int *xPtr = arr, *yPtr = arr + ARRAY_LEN -1;
8 std::cout << *xPtr << ’ ’ << *yPtr; // Should output 10 0
9 return 0;
10 }

from my understanding, line 5 initiates an array of 10 elements, all of which are equal to 10.

Then line 7, *yPtr is a pointer that points to the first element of "arr", plus 10 - 1 ie 9 elements past the first element of "arr", ie the 10th element of the array which is equal to 10. Yet cout << *yPtr outputs 0, not 10. What it do?

Thanks in advance.
Drag007 is offline   Reply With Quote
Old 07-24-2012, 03:05 PM   #2
veteran
 
Drag007's Avatar
 
Join Date: Jul 2005
Posts: 2,014
Re: Simple C++ question

nvrmind just realized line 5 initializes all elements 1-9 as 0
Drag007 is offline   Reply With Quote
Old 07-24-2012, 03:17 PM   #3
old hand
 
sards's Avatar
 
Join Date: Dec 2006
Location: Oklahoma
Posts: 1,463
Re: Simple C++ question

Yes, if you don't specify enough elements in the initialization, the unspecified elements get set to zero.
sards is offline   Reply With Quote
Old 07-24-2012, 04:46 PM   #4
Carpal \'Tunnel
 
Ryanb9's Avatar
 
Join Date: Aug 2006
Location: NEVA!
Posts: 6,369
Re: Simple C++ question

Good job using const int instad of #define tho.
Ryanb9 is offline   Reply With Quote

Reply
      

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



All times are GMT -4. The time now is 03:07 PM.


Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.
Copyright © 2008-2010, Two Plus Two Interactive