Char** == "something" help

R

Rob Smeets

Hi all,

I have the following problem:
I have to revise a c++ dll. And i'm new to c++.
I have to change a function, but i cannot change it's structure.
I want to check the parameters and act according to those parameters.
I've tried several ways to resolve it, but without success.
Could anybody help? Thanks in advance.

Rob Smeets


SomeFunction(const char** pParameters)
{
int i;
bool NTLOGIN;
for (i = 0;pParameters != (char*) 0 && pParameters[i+1] != (char*) 0;i += 2)
{
char* key = pParameters;
char* value = pParameters[i+1];
if key == "ntlogin" // this comparison is never true
{ if value == "TRUE" // this comparison is never true
{
NTLOGIN = TRUE; // this bool is never True
}
}
}
}
 
J

John Harrison

Rob Smeets said:
Hi all,

I have the following problem:
I have to revise a c++ dll. And i'm new to c++.
I have to change a function, but i cannot change it's structure.
I want to check the parameters and act according to those parameters.
I've tried several ways to resolve it, but without success.
Could anybody help? Thanks in advance.

Rob Smeets


SomeFunction(const char** pParameters)
{
int i;
bool NTLOGIN;
for (i = 0;pParameters != (char*) 0 && pParameters[i+1] != (char*) 0;i += 2)
{
char* key = pParameters;
char* value = pParameters[i+1];
if key == "ntlogin" // this comparison is never true
{ if value == "TRUE" // this comparison is never true
{
NTLOGIN = TRUE; // this bool is never True
}
}
}
}


Use strcmp to compare strings, not ==

#include <string.h>

if (strcmp(key, "ntlogin") == 0)

Suggest you buy a book on C++. It has lots and lots of gotchas to catch the
unwary programmer trying to learn C++ on the hoof, as you seem to be. You
just found the first.

john
 
A

Aggro

Rob said:
SomeFunction(const char** pParameters)
{
int i;
bool NTLOGIN;
for (i = 0;pParameters != (char*) 0 && pParameters[i+1] != (char*) 0;i += 2)
{
char* key = pParameters;
char* value = pParameters[i+1];
if key == "ntlogin" // this comparison is never true
{ if value == "TRUE" // this comparison is never true
{
NTLOGIN = TRUE; // this bool is never True
}
}
}
}


Your code doesn't even compile. Use std::string instead of char* and
many of your problems will be solved. ( You can't compare char* using
==, but you can compare std::string using == )

string key = pParameters;
string value = pParameters[i+1];

And put some () around the if-conditions

if( key == "ntlogin" )

if( value == "TRUE" )

And use true, instead of TRUE for bool variable

NTLOGIN = true;
 
P

Pierre Maurette

John Harrison said:
Rob Smeets said:
Hi all,

I have the following problem:
I have to revise a c++ dll. And i'm new to c++.
I have to change a function, but i cannot change it's structure.
I want to check the parameters and act according to those parameters.
I've tried several ways to resolve it, but without success.
Could anybody help? Thanks in advance.

Rob Smeets


SomeFunction(const char** pParameters)
{
int i;
bool NTLOGIN;
for (i = 0;pParameters != (char*) 0 && pParameters[i+1] != (char*) 0;i += 2)
{
char* key = pParameters;
char* value = pParameters[i+1];
if key == "ntlogin" // this comparison is never true
{ if value == "TRUE" // this comparison is never true
{
NTLOGIN = TRUE; // this bool is never True
}
}
}
}


Use strcmp to compare strings, not ==

#include <string.h>

if (strcmp(key, "ntlogin") == 0)

Suggest you buy a book on C++.

One book that don't assume you know C language ;-)
 
D

Default User

Aggro said:
Your code doesn't even compile. Use std::string instead of char* and
many of your problems will be solved. ( You can't compare char* using
==, but you can compare std::string using == )

Oh, you can compare them, it's just that the comparison won't do what
the OP thinks it will. Or it might, in certain situations.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top