Thread: pointers and arrays

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    10

    pointers and arrays

    I am writing a program that checks for the correct matching of parantheses. I am using the "gets" to get the parantheses, and put them in an array. The array is a char and I am trying to use a pointer to access the address or array subscript. with the array being char and the address actually being ints how can I set up the pointer to get the position in the array. pointers are not my strong suit. I have read the tutorial on this site. any help would be greatly apreciated.
    Steve.

  2. #2
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    when you say address, are you talking about subscripts/offsets? post some code

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Why make it hard on yourself? Just use array subscripts for this task...like:


    Code:
    
    
    int next_positition( char string,  int *curr ) {
    
     int pos, temp;
     bool found = false;
    
    for( pos = *curr; string[pos] != NULL; pos++)
    {
     if(string[pos] = '"' )
      {
       found = true; 
       break;
      }
    }
    
    if( !found )
     return -1;
    
    found = false;
    
    for(temp = *curr; string[temp] != NULL; temp++)
    {
     if(string[temp] = '"' )
      {
       found = true; 
      }
    }
     
    if( !found )
      *curr = 0;
    else
      *curr = pos + 1;
    
    return pos;
    }
    
    
    //..............................................................................//
    
    
    
    int main() {
    
    
    char string[] = " \"I think, therefore - I am\" \" ";
    
    int remaining = 0, count = 0;
    
    do{
    
    position = next_position(string, &remaining);
    
    if(position == -1)
    cout << "    None Present" << endl;
    else
     {
      cout << "    Found At Position " << position << endl;
      count++;
     }
    }while(remaining);
    
    if( count )
     {
       cout << count << " Found." << endl;
       count %= 2;
       
       if( count )
       cout << "Mismatched Parenthesis!" << endl;
       else
       cout << "Parenthesis Match!" << endl; 
     }
    
    cin.getline();
    
    return 0;
    }
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM