Skip to main content
gamedev.net
🔒 Locked

Pointers and Arrays

Started by SubImpact 3 replies 1k views
Original Post
SubImpact
SubImpact
Im doing a part in my C++ book about pointers and arrays and this is the example from the book. When I compile and run it it only shows Array of integers 98 at 2359120 -486 at 2359124 301589 at 2359128 Array of chars: Array of doubles: ------------------------------------------------------- Here is the code, what is wrong with it?
[source lang ="cpp"]
#include <iostream>

int main(){
    const unsigned short ITEMS = 3;
    
    int intArray[ITEMS] = {98,-486,301589};
    char charArray[ITEMS] = {'A','B','C'};
    double doubleArray[ITEMS] = {3.14315614, 2.74546944, 7.349e14};
    
    int *intPtr = intArray;
    char *charPtr = charArray;
    double *doublePtr = doubleArray;
    
    std::cout<<"Array of integers:\n";
    
    for (int i; i < ITEMS; ++i){
        std::cout<< *intPtr <<" at "<< reinterpret_cast<unsigned long>(intPtr)<<"\n";
        intPtr++;
        }
        
    std::cout<<"\nArray of chars:\n";
    
    for (int i; i< ITEMS; ++i){
        std::cout<< *charPtr <<" at "<< reinterpret_cast<unsigned long>(charPtr)<<"\n";
        charPtr++;
        }
        
    std::cout<<"\nArray of doubles:\n";
    
     for (int i; i< ITEMS; ++i){
        std::cout<< *doublePtr <<" at "<< reinterpret_cast<unsigned long>(doublePtr)<<"\n";
        doublePtr++;
        }
     
     std::cin.get();
     return 0;
}

SubImpact
SubImpact
Woops, dumb mistake, I messed up on the first for loop and just copyed it and pasted and change the vars. Thanks.
Zahlman
Zahlman
Quote:
Original post by SubImpact
Woops, dumb mistake, I messed up on the first for loop


That's why you don't write for loops when there are standard library algorithms that deal with your situation, or at least iterate using iterators instead of a count variable if possible.

Quote:
and just copyed it and pasted and change the vars. Thanks.


That's why you (much more importantly than above) don't copy-and-paste things, but instead make helper functions.

#include <iostream>#include <algorithm>template <typename T>// Have to pass by reference here, so that '&value' yields the address of the// original rather than a copy.void report(const T& value) {  std::cout << value << " at "             << reinterpret_cast<unsigned long>(&value) << "\n";}template <typename T>void reportArray(T* begin, T* end, const char* type_label) {  std::cout << "Array of " << type_label << "s:\n";  std::for_each(begin, end, report<T>);  std::cout << std::endl;}int main(){    const unsigned short ITEMS = 3;        int intArray[ITEMS] = {98,-486,301589};    reportArray(intArray, intArray + ITEMS, "integer");    char charArray[ITEMS] = {'A','B','C'};    reportArray(charArray, charArray + ITEMS, "char");    double doubleArray[ITEMS] = {3.14315614, 2.74546944, 7.349e14};    reportArray(doubleArray, doubleArray + ITEMS, "double");}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.