Saturday, May 22, 2010

Can a character be passed to an argument to a function when a string is expected? Why or why not? In C?

No, because passing a string is really passing a reference to memory where a sequence of characters ending with a null can be found.





No matter how you cast or otherwise try to fool the compiler, you'll either be passing the character value, or a pointer to the character -- but the function will read past that character into arbitrary memory until it finds a null.

Can a character be passed to an argument to a function when a string is expected? Why or why not? In C?
No, a character or char is different than an string. However, a char can be cased as a string. But when the function requires an argument to be passed in with the type "string", a "char" would give you a errors.





At least, this is the case in C#, I'm assuming the information is valid in both cases. This is very much so the case in Visual C#.Net


No comments:

Post a Comment