Accessing Data of a Pointer to Pointer?

As far as I understand, and I'm probably wrong, in int main(argc, *argv[]) argv is a pointer to a pointer of an array of strings. I want to do something like
1
2
3
4
5
6
7
8
9
10
for(int i = 2; i < argc; i++) {
    if(argv[i] == '1')
    ...
    if(argv[i] == '2')
    ...
    if(argv[i] == '3')
    ...
    if(argv[i] == '4')
    ...
}


But I get the error that I can not convert from int to char*. How would I access the actual value in argv? I thought about *argv but wouldn't that just return the memory address of the thing pointed to by the pointer argv is pointing to? Also, I am not sure to use '1' or "1", because it seems like it points to an array of strings but it is declared as a pointer to char? Any help is appreciated.
closed account (S6k9GNh0)
The second parameter of main is actually an array of C strings. C strings are an array of characters themselves so in the long view, so the second parameter is actually a two dimensional array. To access a specific character one may do the following:
 
argv[i][x]; //i is the number parameter, where x is the number of character in the sequence. 


You could also use some functions in the standard string library:

http://cplusplus.com/reference/clibrary/cstring/strpbrk/
http://cplusplus.com/reference/clibrary/cstring/strrchr/
http://cplusplus.com/reference/clibrary/cstring/strstr/

Or you may turn the parameters into std::strings and use the C++ STL library.
Last edited on
What about if I want to locate "/?" within a cstring? Is it possible to search for an explicit string in a cstring? I saw strpbrk does that, but it looks for '/' and '?', I just want "/?"

EDIT: Fixed it with this:
1
2
3
4
5
6
7
8
char * found, * found2;
//.... code
found = strpbrk(argv[1], "/");
found2 = strpbrk(found + 1, "?");
if(found2 - found == 1) {
	//... code
	return 0;
}


for some reason though, it did not work when I tried this:

1
2
3
4
5
6
7
char * found;
//... code
found = strpbrk(argv[1], "/");
if(found + 1 == "?") {
        //... code 
        return 0;
}


I don't know why.
Last edited on
argv is an array of pointers to char arrays (strings).
argv[i] is a char array (string).
argv[i][j] is a char (a character)

With argv[i] == '1', You are comparing a char array (string) with a char.
The poor old compiler can't do what it instinctively wants to do, which is convert '1' to an int, then to a char array (string), but it can't; so it's telling you it can't.

You can search for a string in another string with strstr. It either returns the string it found or NULL.
Last edited on
Array indexes start at 0, in C++. I see that the for loop in the original post is starting at index 2, that should probably be a 1 (0 would be the name of the executable).
Topic archived. No new replies allowed.