Sign in to follow this  

char is an array or single character ?

This topic is 4728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Apparently strings are arrays of char's ? Right ? Well, I'm just a bit confused why a single char can hold a string, like this:
  const char* P1_AI = "P1_AI"; /* */
  const char* P2_AI = "P2_AI"; /* */
  
  int i;
  for(i = 0; i < argc; i++)
  {
    if (*argv[i] == *P1_AI) {Player_1_AI = 1;}
    if (*argv[i] == *P2_AI) {Player_2_AI = 1;}
  }
Should the char's not only hold a single character?

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
You aren't doing string comparisons, you're comparing the first letter in each string.
Thats what I originally thought, but earlier the command line options were working as they should and now theyre not :S wierd.

Thanks mike25025 :)

Share this post


Link to post
Share on other sites
Also, now I'm using the following:
int main(int argc, char *argv[])
{

const char P1_AI[] = "P1_AI";
const char P2_AI[] = "P2_AI";

int i;
for(i = 0; i < argc; i++)
{
if (strcmp(argv[i],P1_AI) == 0) {Player_1_AI = 1;}
if (strcmp(argv[i],P2_AI) == 0) {Player_2_AI = 1;}
}

Why can I do the string comparison using just "argv[i]" and not "*argv[i]" ? does the star not allow you to access the value that the pointer is pointing to?

Share this post


Link to post
Share on other sites
argv[i] is of type pointer to char and *argv[i] is a single char. strcmp() takes as parameters pointers to char, so obviously you wouldn't be able to pass something of type char to strcmp().

Share this post


Link to post
Share on other sites
Yep. Dereferencing a char * yields a single character. A pointer to char only implies the presence of a null-terminated string by convention. The whole concept doesn't exist in the mind of the compiler, only in the mind of the standard C library functions. The char * points to the first character, and it is blindly assumed (by those library functions, as well as anything else you write which accepts a char * and treats it as a "string") that:

a) The memory following right after that character belongs to your process
b) It continues to be memory belonging to you right up until the first occurance of '\0'
c) The data up until that '\0' is your "string".

Pretty dangerous, yes? Chars really are numeric types; the idea that they could possibly represent text dates back to the ignorant age of ASCII and teletypes. And you never hear of people using int*'s or short*'s in similar fashion do you? (Could it be because 0 is a commonly useful value for numeric types that can't be spared to mark an "end" condition?)

So now you begin to see the benefit of letting std::string wrap things up for you, yes? [smile] Of course, C++ doesn't wrap the input args for you, but you should be able to do this:


int useAI[2] = {0};

int main(int argc, char *argv[]) {
const char* AI_flags[2] = { "P1_AI", "P2_AI" };
// these don't need to be strings for the comparison...

for(int i = 0; i < argc; i++) for (int j = 0; j < 1; j++)
if (string(argv[i]) == AI_flags[j]) { useAI[j] = 1; }
}


(Disregard that if you're using C ;) Though I thought C didn't provide const correctness?)

Share this post


Link to post
Share on other sites

This topic is 4728 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this