Elysia you know what are you doing but im not
If you did similar things, just give me an idea or hint! ( ex: can I do it without using pointers )
in my humble opinion, pointer is not for me
Thanks in advance
Elysia you know what are you doing but im not
If you did similar things, just give me an idea or hint! ( ex: can I do it without using pointers )
in my humble opinion, pointer is not for me
Thanks in advance
Arrays can act like pointers, but if you do it right, there will not be a problem. You must learn pointers sooner or later (or you can forget C altogether).
To compare two strings (be they stored in pointer or arrays doesn't matter), use strcmp. As simple as that.
When you print using printf() and %s format, you are not printing the address of the string, are you? If you doDo you still get the same value? Probably not. That is what "usernam == usernameLoginP" is comparing - and if they aren't the same in the printout, then they are also not the same in the comparison.Code:printf("username = %p, usernameLoginP=%p", username, usernameLoginP);
strcmp() does something along the lines of:
This walks through the two arrays (or blocks of memory) until it finds either the end of the first string, or the strings are different (which also happens if the second string is shorter than the first string). That is different from comparing s1 with s2 - that's the location of the string.Code:int strcmp(char *s1, char *s2) { while(*s1 && *s1 == *s2) // Exit when we hit a "zero", or when the characters are different. { s1++; s2++; } return *s2 - *s1; // If they are different, return a signed indication of which is greater. }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
hımm I understood it now.
is it possible to convert username and usernameLoginP pointer to stringCode:printf("usernameLoginP=%p\n",usernameLoginP); //0040,,,, printf("username=%p\n",username); //0022cc... if( strcmp(username, usernameLoginP ) == 0 ) // comparing the addresses { printf("Correct!"); }
I dont know to escape from pointer
A string in C is "a block of characters that end with a zero-value" - so a string is a pointer or array - and since arrays and pointers appear to be very similar in most aspects, you can most of the time use them interchangably [but beware that they ARE NOT THE SAME THING - just that they appear to be most of the time].
You comment "// comparing the addresses" is incorrect - strcmp() is comparing the CONTENT of the two strings passed in (as the address of the first character).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.