Connecting Tech Pros Worldwide Help | Site Map

alternative for pointers

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 11:32 PM
stanlo
Guest
 
Posts: n/a
Default alternative for pointers

Hello to all, i have a progam fragment here for the follow up of my
project ; mathematicl expression. i don t want to use pointers, this is
the fragment.my problem is there an alternative way of declaring and
defining my functions without using pointers,eg in the goToOp1
function, i don t wish to write "char*line".
this function gets the first operator in the mathematical expression
like 1+6*6/2.

//function declarations

int goToOp1(char* line, int startPos, int &curExprBegin);

int goToOp2(char* line, int startPos, int &curExprEnd);

void reduceArr(char* line, int curExprBegin, int curExprEnd, long
number);

void discardBadChar(char* line, bool &exit);

void errorOutput(const char* erroralert);

//function definitons, i did just the definition of one of the
functions

int goToOp1(char* line, int startPos, int &curExprBegin)
{
int pos=startPos-2;

int op1Size=0;

int answer;

answer = 0;


  #2  
Old July 22nd, 2005, 11:32 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: alternative for pointers

"stanlo" <mungwest@yahoo.com> wrote in message
news:1105307821.732625.54300@c13g2000cwb.googlegro ups.com...[color=blue]
> Hello to all, i have a progam fragment here for the follow up of my
> project ; mathematicl expression. i don t want to use pointers, this is
> the fragment.my problem is there an alternative way of declaring and
> defining my functions without using pointers,eg in the goToOp1
> function, i don t wish to write "char*line".[/color]

If you insist upon using 'C-style' strings, pointers are
the only way to pass them to functions. However the C++
standard library provides a 'string' type (declared by standard
header <string>). Look it up in your C++ textbook.
[color=blue]
> this function gets the first operator in the mathematical expression
> like 1+6*6/2.
>
> //function declarations
>
> int goToOp1(char* line, int startPos, int &curExprBegin);[/color]

int goToOp1(const std::string& line,
std::string::size_type startPos,
int &curExprBegin);
[color=blue]
>
> int goToOp2(char* line, int startPos, int &curExprEnd);[/color]

int goToOp2(const std::string& line,
std::string::size_type startPos,
int &curExprEnd);
[color=blue]
>
> void reduceArr(char* line, int curExprBegin, int curExprEnd, long
> number);[/color]

void reduceArr(const std::string& line,
int curExprBegin,
int curExprEnd,
long number);

BTW why do some of these take references to int, and some
int by value?
[color=blue]
>
> void discardBadChar(char* line, bool &exit);[/color]

void discardBadChar(const std::string& line, bool &exit);
[color=blue]
>
> void errorOutput(const char* erroralert);[/color]

void errorOutput(const std::string& erroralert);
[color=blue]
>
> //function definitons, i did just the definition of one of the
> functions
>
> int goToOp1(char* line, int startPos, int &curExprBegin)[/color]

int goToOp1(const std::string& line,
std::string::size_type startPos,
int &curExprBegin)
[color=blue]
> {
> int pos=startPos-2;[/color]

std::string::size_type pos(startPos-2);

Why subtracting 2?
[color=blue]
>
> int op1Size=0;
>
> int answer;
>
> answer = 0;[/color]

Why not simply:

int answer(0);

Without more context, I cannot comment on whether your
other argument types and return types are appropriate.

-Mike


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.