Confused

This is a discussion on Confused within the C Programming forums, part of the General Programming Boards category; #include <stdio.h> int main(void) { char a="a"; return 0; } what's wrong of that ? compile error : Cannot convert ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    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.......

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    string a='abcdef';
    string b="abcdef";

    they are also different because b contain a null terminator and a does not, am I right?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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]

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    confused (2)

    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...

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: confused (2)

    >"/?" 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]

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    Amazing ! Please take a look also

    Code:
    #include <stdio.h>
    
    int main(void) {
      printf("%d , %d",sizeof("/?"), sizeof('/?'));
      return 0;
    }
    the output is 3, 4 why ?

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    85
    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;
    }
    I see you point!
    thanks^^

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    Re: Re: confused (2)

    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).
    does that mean if the parameter is array, then need not to pass a pointer even other type of variables (e.g. int, long)?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >"/?"
    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]

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Re: Re: confused (2)

    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)?
    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);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    Re: Re: Re: Re: confused (2)

    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 successfully
    Code:
      char a[]="abc";
      char b='a';
      strcmp(a,&b);
    //compile successfully
    Code:
    cout <<&a;  //no error when compile, print out a address
    then just one question: why I can't pass &a into strcmp() ?

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Re: Re: Re: Re: confused (2)

    >char a[]="abc";
    >char b='a';
    >strcmp(&a,&b);
    >then just one question: why I can't pass &a into strcmp() ?

    Because:
    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.
    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:
    Code:
    char a[] = "Hammer";
    char b[] = "Kelvin";
    strcmp(a, b);
    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.

    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]

  14. #14
    Registered User
    Join Date
    Jul 2002
    Posts
    85

    Re: Re: Re: Re: Re: Re: confused (2)

    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:
    Code:
    char a[] = "Hammer";
    char b[] = "Kelvin";
    strcmp(a, b);
    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.

    Also, cout is C++. This is a C board, where we only talk about C.
    got it! sorry><

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 04:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 12:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21