#include <stdio.h>
int main(void) {
char a="a";
return 0;
}
what's wrong of that ?
compile error : Cannot convert 'char *' to 'char' in function main()
after replacing with this, it has no error:
char a=(char)"a";
I am really confused.......
#include <stdio.h>
int main(void) {
char a="a";
return 0;
}
what's wrong of that ?
compile error : Cannot convert 'char *' to 'char' in function main()
after replacing with this, it has no error:
char a=(char)"a";
I am really confused.......
A char variable holds 1 character only. When you use "a", you are using a string of 2 chars, the a and a null terminator.
To assign a single character, use single quotes. EG
>char mychar = 'a';
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
string a='abcdef';
string b="abcdef";
they are also different because b contain a null terminator and a does not, am I right?
>am I right?
No.
What's that "string" type? There's no such thing in C (but there is one in C++).
You cannot define character constants this way either:
>a='abcdef';
If you want a char array without a NULL terminator, you can do so this way:
>char a[] = {'H','a','m','m','e','r'};
For example:
Code:#include<stdio.h> int main() { int i; char a[] = {'H','a','m','m','e','r'}; for (i = 0; i < sizeof (a); i++) putchar(a[i]); return 0; }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
int StrCmp(
LPCTSTR lpStr1,
LPCTSTR lpStr2
);
that means strcmp requires two pointer parameters.
but why strcmp(&a, "/?"); can compile successfully without any error and warning? "/?" is not a point ....
I am totally confused
Please help...
>"/?" is not a point(er) ....
Yes, it is. Well, kind of. When you hardcode a string like this
>"My Name"
you are creating a char array. The compiler will see this as a pointer to an array of chars containing 8 bytes of data (7 chars + 1 null).
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
the output is 3, 4 why ?Code:#include <stdio.h> int main(void) { printf("%d , %d",sizeof("/?"), sizeof('/?')); return 0; }
I see you point!Originally posted by Hammer
>am I right?
No.
What's that "string" type? There's no such thing in C (but there is one in C++).
You cannot define character constants this way either:
>a='abcdef';
If you want a char array without a NULL terminator, you can do so this way:
>char a[] = {'H','a','m','m','e','r'};
For example:
Code:#include<stdio.h> int main() { int i; char a[] = {'H','a','m','m','e','r'}; for (i = 0; i < sizeof (a); i++) putchar(a[i]); return 0; }
thanks^^
does that mean if the parameter is array, then need not to pass a pointer even other type of variables (e.g. int, long)?Originally posted by Hammer
>"/?" is not a point(er) ....
Yes, it is. Well, kind of. When you hardcode a string like this
>"My Name"
you are creating a char array. The compiler will see this as a pointer to an array of chars containing 8 bytes of data (7 chars + 1 null).
>"/?"
This is 3 because there are 2 char, plus 1 null terminator.
>'/?'
This is not a valid character constant, and is therefore not really valid coding. However, the compiler is storing it as a character constant on your behalf, and says will take 4 bytes to store. You get the same effect if you use
>'A' - This also gives a sizeof of 4.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I'm not sure I understand your question, but I'll have a go anyway:Originally posted by Kelvin
does that mean if the parameter is array, then need not to pass a pointer even other type of variables (e.g. int, long)?
If the function takes an array as a parameter, you must pass an array name or a pointer to that function. Note that array names are really pointer is disguise.
Eg:
>void myfunc(char myarray[]);
To call this you can:
>char a[10];
>myfunc(a);
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Originally posted by Hammer
I'm not sure I understand your question, but I'll have a go anyway:
If the function takes an array as a parameter, you must pass an array name or a pointer to that function. Note that array names are really pointer is disguise.
Eg:
>void myfunc(char myarray[]);
To call this you can:
>char a[10];
>myfunc(a);Code:char a[]="abc"; char b='a'; strcmp(&a,&b); //can not compile successfullyCode:char a[]="abc"; char b='a'; strcmp(a,&b); //compile successfullythen just one question: why I can't pass &a into strcmp() ?Code:cout <<&a; //no error when compile, print out a address
>char a[]="abc";
>char b='a';
>strcmp(&a,&b);
>then just one question: why I can't pass &a into strcmp() ?
Because:
Also, the b variable is incorrect, you should not pass this to strcmp(), because it is not a string, it's only a single char. The compiler won't complain, because you are passing &b, which is the address of b (and hence is a pointer). But your program will crash, or at best, produce unexpected results.If the function takes an array as a parameter, you must pass an array name or a pointer to that function. Note that array names are really pointers in disguise.
This is the correct method:
Note, this code doesn't do anything with the return code from strcmp(), and therefore does nothing. To actually use it properly, you embed it in a if statement.Code:char a[] = "Hammer"; char b[] = "Kelvin"; strcmp(a, b);
Also, cout is C++. This is a C board, where we only talk about C.
Last edited by Hammer; 07-12-2002 at 05:33 AM.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
got it! sorry><Originally posted by Hammer
>char a[]="abc";
>char b='a';
>strcmp(&a,&b);
>then just one question: why I can't pass &a into strcmp() ?
Because:
Also, the b variable is incorrect, you should not pass this to strcmp(), because it is not a string, it's only a single char. The compiler won't complain, because you are passing &b, which is the address of b (and hence is a pointer). But your program will crash, or at best, produce unexpected results.
This is the correct method:
Note, this code doesn't do anything with the return code from strcmp(), and therefore does nothing. To actually use it properly, you embed it in a if statement.Code:char a[] = "Hammer"; char b[] = "Kelvin"; strcmp(a, b);
Also, cout is C++. This is a C board, where we only talk about C.