The only reason I could think that there would be a difference is if the memory model you're using makes s[] a far pointer, and *s a near pointer. You're asking for the size of the pointer, not the size of the string anyway, so that must be the difference. I'm not willing to bet but it's what I would guess.
I don't think that the \ has anything to do with it. Shouldn't you be using malloc anyway? When you declare *s, you only allocate a pointer, you don't actually allocate memory, same with s[], right? Otherwise, where are you putting the data?
As an answer to your original question, the difference is in pointer size, which I believe is a near/far pointer difference.
MWB. As always, I hope that helped!
Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
Same sequence of characters, except for s is now a pointer. sizeof calculates the size of the pointer itself, which on your implementation is 2 (on mine it's 4). You would get the same result with the expression:
printf("%d\n",sizeof(char *));
It is perfectly ok to initialize a pointer like this BTW. The compiler creates storage for the string literal and sets s to point to the first element.
Pedantic Note 1:
main should return an int and be of the form:
int main() or int main(int,char **)
The results of returning void from main are undefined and therefore may result in your program crashing (or little green men invading your home and abducting your family).
Pedantic Note 2:
In your sizeof expressions the parentheses aren't necessary as sizeof is an operator, not a function, so:
printf("%d",sizeof s);
The parentheses /are/ necessary for type names:
printf("%d",sizeof(char));
MWB, if you were betting, I'd be willing to take you up on it ;-)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.