Why does this work?

This is a discussion on Why does this work? within the C++ Programming forums, part of the General Programming Boards category; Code: // Phone list #include <iostream> #include <string> using namespace std; string getSearchStr(); void getEntry(string, string *, int); int main() ...

  1. #1
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    307

    Why does this work?

    Code:
    // Phone list
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string getSearchStr();
    void getEntry(string, string *, int);
    
    int main()
    {
        string phoneList[] = { "Becky Warren, 678-1223", "Joe Looney, 586-0097",
                              "Geri Palmer, 223-8787",  "Lynn Presnell, 887-1212",
                              "Holly Gaddis, 223-8878", "Sam Wiggins, 486-0998",
                              "Bob Kain, 586-8712",     "Tim Haynes, 586-7676",
                              "Warren Gaddis, 223-9037","Jean James, 678-4939",
                              "Ron Palmer, 486-2783"
                             };
        
        getEntry(getSearchStr(), phoneList, sizeof(phoneList) / sizeof(string));
        cout << "Press enter to continue. . .";
        cin.ignore();
        cin.get();
        return 0;
    }
    
    string getSearchStr()
    {
        string name;
        
        cout << "Enter search criteria: ";
        cin >> name;
        
        return name;
    }
    
    void getEntry(string name, string * list, int size)
    {
        bool match = false;
        
        for (int i = 0; i < size; i++)
        {
            if (list[i].find(name, 0) != string::npos)
            {
                match = true;
                cout << list[i] << "\n";
            }
        }
        
        if (!match)
            cout << "Did not find a matching name.\n";
    }
    The question is why does sizeof(phoneList) / sizeof(string) return the right number of elements in the phoneList array? Each string is a different length so how does sizeof(string) get the right length?? I know how it works with ints and doubles and other built-in data types so I figured I'd try it here and it works. Just...how?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Well a string is a fixed sized object, the variable part of the object (ie the string itself) is typically stored in allocated memory.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Posts
    307

    Ok

    Ah I was thinking that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21