Comparing char array for two strings

This is a discussion on Comparing char array for two strings within the C++ Programming forums, part of the General Programming Boards category; I have a command line structure, and I was wondering how I could compare a char array for two separate ...

Comparing char array for two strings

I've tested that (not that \exact\ code), and it doesn't work. Well, if it does, I'm using it wrong.

I want to pass two command line options onto another function, but can't figure out a way to check to see if one is accompanying the other so that I can send them together, because alone they won't work.

So, how would I go about doing that? I've set up about a million for statements, and realized they were all destined for uselessness and I canít think of anything else to do.

If argv happens to be the typical second parameter to main, you might notice that it is not a pointer to a char, but a pointer to a pointer to a char -- a pointer to a string. Comparing C-style strings is not done using ==, as mentioned previously.

I was wondering if I could search for two separate characters in the same array.

Of course. One way would be to search the whole array for one char, and then search the whole array for the other char. But that's pretty inefficient, so during one pass of the array, you could count how many of each letter you found. If each count was greater than 1, then both letters were present in the array.

However, as Dave_Sinkula pointed out, argv[] is an array of pointers. Furthermore, argv[0] is a pointer to the program name, and so it does not point to any user input. The pointer argv[1] is going to be the first pointer that could possibly point to user input. A pointer to a string points to the first character in the string, so you can use pointer arithmetic or array notation to access the individual characters in the string. If you know that a variable named "myVar" is a pointer to a char array, how would you access the first character in the array? Now substitute argv[1] for myVar.

This is not going to work:

Code:

if( argv[j] == 'A' && argv[j] == 'Z')

because:

1) The value of a pointer is never going to be equal to a character. A pointer stores an address, which looks something like 8567A9D.

2) Something cannot be equal to both 'A' and 'Z', so the if statement will always fail.

Comparing C-style strings is not done using ==, as mentioned previously.

I don't think anything in his original post or his revised post should be interpreted to require comparing c-style strings.