[英]What would be the difference between setting a struct sockaddr_in to 0 and '\0' using memset?
I have just started off with C socket programming. 我刚开始使用C套接字编程。 I have read that 0 as an integer constant, refers to a null pointer when compared with a pointer and '\\0' refers to a null character, something that sets all the bits to 0. And, in the case concerning my question, I know I should probably be using '\\0'.
我已经读过0作为整数常量,与指针相比是指空指针,而'\\ 0'是指空字符,它将所有位设置为0。并且,在涉及我的问题的情况下,我知道我可能应该使用'\\ 0'。 But I can see a lot of implementations that uses 0 instead.
但是我可以看到很多使用0的实现。 What would be the difference as such?
这样有什么区别? I don't wanna go with whatever that works.
我不想接受任何可行的方法。 I can't move on till I understand why.
在我明白原因之前,我无法继续前进。 Thanks!
谢谢!
There is no difference. 没有区别。 They are equal.
他们是平等的。
There will be absolutely no difference. 绝对没有区别。 Please use
0
since you zeroise memory and you don't want to underscore any nature of the memory like you would do, say, when comparing either element of a char
array with '\\0'
to denote that you are looking for a null character . 请使用
0
因为您将内存归零,并且不想像在将char
数组的任一元素与'\\0'
以表示您要查找空字符时那样强调内存的任何性质。 。
To be more precise, ' '
is an expression which gives an integer constant which corresponds to the character specified. 更准确地说,
' '
是一个表达式,它给出一个与指定字符相对应的整数常量。 In this particular case, '\\0'
evaluates to the same integer - 0
. 在这种特殊情况下,
'\\0'
等于相同的整数0
。 So, no difference. 因此,没有区别。
They are equivalent. 它们是等效的。
But it is common to use 0
when working with integers, and '\\0'
when dealing with characters or bytes: 但是在处理整数时通常使用
0
,而在处理字符或字节时通常使用'\\0'
:
int n = 0;
char ch = '\0';
For pointers use NULL
macro. 对于指针,请使用
NULL
宏。
memset
has following prototype: memset
具有以下原型:
void * memset ( void * ptr, int value, size_t num );
Although value
has int
type, it is being interpreted as byte value. 尽管
value
具有int
类型,但它被解释为字节值。 So there is no difference. 因此没有区别。
In C, character literals such as 'x'
have type int
, which means '\\0'
and 0
are not just numerically equal, they are 100% equivalent semantically. 在C语言中,诸如
'x'
类的字符文字具有int
类型,这意味着'\\0'
和0
不仅在数值上相等,而且在语义上是100%等效的。 They are two different spellings of the same integer constant. 它们是相同整数常量的两个不同拼写。 You can only tell the difference using a construct that inspects the spelling of tokens, such as the preprocessor's
#
and ##
operators. 您只能使用检查令牌的拼写的结构(例如预处理器的
#
和##
运算符)来区分差异。
(Yes, this means '\\0'
is a null pointer constant.) (是的,这意味着
'\\0'
是一个空指针常量。)
char
is an integer type, it means that the characters you can use with the simple quotes like 'a'
are in reality integer values. char
是整数类型,这意味着您可以使用简单引号(例如'a'
使用的字符实际上是整数值。 I invite you to look for the ascii
, you'll see the link between characters and integers. 我邀请您查找
ascii
,您将看到字符和整数之间的链接。 You can do some easy tests with printf()
: 您可以使用
printf()
做一些简单的测试:
printf("%c == %d == %c\n", 'A', 'A', 65);
You'll notice that the character '\\0'
has the integer value 0
, so there is no difference, you can use 0
or '\\0'
in your code, for the compiler it's the same thing. 您会注意到字符
'\\0'
具有整数值0
,所以没有区别,您可以在代码中使用0
或'\\0'
,对于编译器而言,这是相同的。 Usually, NULL
is a macro for (void *) 0
wich is the value 0
casted into generic pointer (it's still the value 0
, but it will be interpreted as a pointer type). 通常情况下,
NULL
是一个宏(void *) 0
至极是值0
浇铸成通用指针(它仍然是值0
,但它会被解释为指针类型)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.