strcmp

hi there,
a newbie is back to ask some newbie question again =)..

i have a line of code:
1
2
user *temp, *temp2;
if (strcmp(temp->username[0], temp2->username[0]) != 0)


where temp and temp2 is struct.
1
2
3
4
struct user{
	char username[1];
	user *next;
};


and i get error for it.
cannot convert parameter 1 from 'char' to 'const char *'

I dun really understand what is about "const" i did my research but still dun understand pls explain.

Thanks for reading...
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
}
Last edited on
temp->username[0] has type "char", right?

strcmp() wants as its two parameters C strings which are of type const char* (or just char* for
the sake of a newbie).
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?
When you add the [0], you are no longer getting an array, rather just a single character out of it.
I'm surprised nobody mentioned this yet.

1
2
3
4
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 
Last edited on
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?
You can pretty much prefer string everywhere. Unless you are in C, which doesn't have them.
Topic archived. No new replies allowed.