Jump to content
  • Advertisement
Sign in to follow this  
xyuri

char is an array or single character ?

This topic is 5013 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 == *P1_AI) {Player_1_AI = 1;}
    if (*argv == *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
Advertisement
a single char cant hold a string
and are you trying to compare two strings?
use strcmp(str1,str2)==0 instead of *str1==*str2

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,P1_AI) == 0) {Player_1_AI = 1;}
if (strcmp(argv,P2_AI) == 0) {Player_2_AI = 1;}
}

Why can I do the string comparison using just "argv" and not "*argv" ? 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 is of type pointer to char and *argv 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
Aaaah, thats right ... because an array is really a pointer :) Thank you very much dude :)

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) == 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
Woa, that really helped me see how things work a little ... :-)

Thank you very much dude. Is much appreciated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!