Thread: Array/Pointer question

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question Array/Pointer question

    Hi,

    Had 2 questions about this little program I typed in from a book I'm using to learn C++:

    Code:
    /* The pattern matching algorithm uses 2 loops.  The outer loop
    is controlled by the pointer p1 which points to elelments in array
    a1 where the inner loop will begin checking for a match with array
    a2.  The inner loop is controlled by the integer j which is used
    to compare corresponding elelments of the 2 arrays.  If a mismatch
    is found, the inner loop aborts and the outer loop continues by
    incrementing p1 to look for a match starting with the next element
    of a1.  If the inner loop is allowed to finish, then the condition
    (j = n2) will be true and the current location pointed to by p1 is
    returned.  The test driver verifies that the match has indeed been 
    found by checking the actual addrsses.  */
    
    
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    short* loc(short* a1, short* a2, int n1, int n2)
    { short* endl = a1 + n1;    //  ???? if a1 = a[0], 
      // then wouldn't this make endl point to a[9]?  
      // Isn't a[0] = a[0] ... a[8]??
      for (short* p1 = a1; pl < endl; pl++)
        if (*pl == *a2)
        { int j;
          for (j = 0; j < n2; j++)
            if (p1[j] != a2[j]) break;
          if (j == n2) return p1;   // ??? if j == n2 (or 5), how can
          // that be since the for loop only allows j < n2?
        }
      return 0;
    }
    
    int main()
    { short a1[9] = {11, 11, 11, 11, 11, 22, 33, 44, 55};
      short a2[5] = {11, 11, 11, 22, 33};
      cout << "Array a1 begins at location:\t" << a1 << endl;
      cout << "Array a2 begins at location:\t" << a2 << endl;
      short* p = loc(a1, a2, 9, 5);
      if (p)
      { cout << "Array a2 found at location:\t" << p << endl;
        for (int i = 0; i < 5; i++)
          cout << "\t" << &p[i] << ": " << p[i]
               << "\t" << &a2[i] << ": " << a2[i];
      }
      else cout << "Not found.\n";
      system("pause");
      return 0;
    }
    // ???? if a1 = a[0],
    // then wouldn't this make endl point to a[9]?
    // Isn't a[0] = a[0] ... a[8]??


    // ??? if j == n2 (or 5), how can
    // that be since the for loop only allows j < n2?

    Dumb question or not, it just really confused me when I thought I almost completely understood the way this piece of code worked. I appreciate your help.

    Swaine777

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > ???? if a1 = a[0],
    > then wouldn't this make endl point to a[9]?
    > Isn't a[0] = a[0] ... a[8]??

    Yes, endl would point to a1[9]. And since there is no a1[9], it points one location past the end of the array. So once p1 points one location past the end of the array, the loop will finish.
    You are correct: the array goes from a1[0] to a1[8].


    > ??? if j == n2 (or 5), how can
    > that be since the for loop only allows j < n2?

    This if statement is not within the for-loop:
    for (j = 0; j < n2; j++)
    So the if() statement executes once the for-loop is done. So if all five elements of a2 match, this if() statement will be true, and p1 will be returned.

    >Dumb question or not, it just really confused me when I thought I almost completely understood the way this piece of code worked. I appreciate your help.

    There are no dumb questions. We're all here to learn.
    Last edited by swoopy; 05-17-2003 at 12:27 AM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Thanks a lot swoopy. Hehe, if arrays and pointers are this confusing, wait till I get to vectors! P

    Swaine777

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM