silhouette hustler
silhouette hustler

Reputation: 1763

Pass the previously assigned pointer value to a loop

I've got a string pointer string* ifxPtr;, what I'm trying to do is to set this pointer to point to the user input and then loop through each character of it.

string* ifxPtr;

void Expression::GetInfix(string data) // This method runs first and it's setting the pointer to point to previously passed user input
{   
   ifxPtr = &data;
   ConvertToPostfix();
}

void Expression::ConvertToPostfix()
{
    for (int i = 0; i < *ifxPtr->length; i++) // Here's the struggle, compiler is complaining about *ifxPtr with message Cannot apply binary '<' to 'int' and 'unassigned() const'
    {
        //Do something here
    }
}

Upvotes: 2

Views: 46

Answers (3)

Hatted Rooster
Hatted Rooster

Reputation: 36503

foo-> is shorthand for (*foo).. Don't double up operators, also it should be length():

for (int i = 0; i < ifxPtr->length(); i++)

Also, be careful with that design, possibility of running into Undefined Behaviour with a simple mistake is big.

Upvotes: 1

Reshad
Reshad

Reputation: 239

if you use and *before it means using the value which is pointed by the pointer.

string s,*p; p=&s;

here *p.lenght() is same as s.length if you want to access through pointer you have to use it like this p->length().

Upvotes: 0

Justin Lam
Justin Lam

Reputation: 799

  1. length is a function and it should be length()
  2. You don't need to deference the pointer if you use ->
  3. The result returned from length() is size_t

Here is what I would use:

int length = static_cast<int>((*ifxPtr).length());

Upvotes: 3

Related Questions