strcmp is for comparing strings but you are passing a single character by value to a function that expects a pointer.
Your username is one character in length which seems strange. You can compare characters directly. if(temp->username[0] == temp2->username[0])
If they were strings you could do this. But they aren't. With only 1 character there is no room for at least one character plus the null. Did you mean for the array to be of a bigger size?
1 2 3 4 5 6 7 8
if (strcmp(temp->username, temp2->username) != 0)
{
// not equal
}
else
{
// equal
}
yea i got it if (strcmp(temp->username, temp2->username) != 0)
This is what i want, but can explain whats the different between the code and code below if (strcmp(temp->username[0], temp2->username[0]) != 0)
I tot I has declay "username" as array rite?
so by comparing the 1st value isn't it a correct way?
struct user{
char username[1]; // only 1 character in size!
user *next;
};
This means your buffer is only large enough to hold a single character. Note that C style strings need to be null terminated to mark the end of the string, which means that 'username' only has enough room for ZERO characters. IE: it can't hold anything and is pretty much completely worthless.
If you try to put a larger string (ie: more than zero characters) in it, you'll have a buffer overflow, memory corruption, and all sorts of other terribly nasty problems.
The easy solution here is to use std::string to represent strings, rather than char arrays:
1 2 3 4 5 6 7 8 9 10
struct user{
string username; // use std::string instead
user *next;
};
// now you don't need strcmp, either, you can compare with ==
if(temp->username == temp2->username)
{
// equal
}
if you must use a char array, you'll need to make it bigger than [1]:
char username[100]; // can now have a name up to 99 characters long, but no longer
oh i see, it is good to use string then char.
when i use char it's so complicated to convert here and there = =
and i used string, everything gets easy.
but now i a question.
when do we use char? and when do we use string?
or just simply depend on programmer?