Summary
1) In general engineering development, is required to disable the goto statement. Different compilers may have different error handling for the goto statement.
2) In C language, if the function does not write a return value, the default return value type of is int; if the function does not write a parameter list, the function
accepts any number of parameters.
Therefore, if the function does not return a value, you must explicitly declare the return value type as void; if the function has no parameters, you must declare the parameter list as void.
3) Void is a basic type, but is not a basic data type, so
cannot be used to define variables (the C language does not specify how much void is an alias for memory space).
4) For standard C language specification, sizeof(void) is not allowed, such as bcc compiler; for
extended C language, sizeof(void) may be allowed, such as gcc compiler, the output is 1.
5) The main function of the void*
used to receive any type of pointer. In C language,
void*
and type*
can be converted to each other; in C++, void*
can be used as an lvalue to receive any type of pointer, when used as an rvalue, must be forced to convert.
goto and void
1、goto
The goto statement has strong assembly language features and can jump to the specified statement, just like the jump instruction of assembly language.
In engineering practice, generally disables "goto". Goto will destroy the structure of the program and bring unexpected errors in
.
// 代码示例:以下代码输出什么?
int main()
{
goto End;
int i = 0;
End:
printf("i = %d\n", i);
return 0;
}
- gcc compiler: Compile error
error
, suggesting that the initialization of the i variable was skipped. - VS compiler: Compiler warning
warning
, indicating that i is not initialized, but an executable program has been compiled. The result of the operation is a random value, an unexpected error!
2、void
2.1 Void modifies the return value and parameters
In C language:
- If the function does not return a value, it should be declared as void
- If the function has no parameters, its parameters should be declared as void
Note that in C language:
- If the function does not write a return value, the default return value type of
If the function does not declare a parameter list,
accepts any number of parameters by default
f() { } int main() { int i = f(1, 2, 3); // ok return 0; }
2.2 Is there a variable of type void?
Conclusion: does not exist a variable of type void
Void is a basic type, but is not a basic data type; the C language does not define how much memory void is an alias, so the corresponding variable cannot be defined.
int main()
{
void var; // error
void arr[5]; // error
return 0;
}
Tips:
ANSI C
: Standard C language specification, such as bcc compilerextended C: extended on the basis of ANSI C, such as gcc compiler
// void类型有大小么? int main() { prinft("%d\n", sizeof(void)); return 0; }
- gcc compiler, Demo can be edited, the output is 1
- Bcc compiler, compile error, prompt void is not allowed type.
2.3 Pointer of void type
In the C language, the pointer type check is not so strict, so the pointer of the void type
can be converted with the pointer of any data type
.
// test.c int main() { int* p = nullptr; void* pv = p; // ok char* pc = pv; // ok return 0; }
In the C++ language, the
type has been enhanced, and colleagues are also compatible with the writing in the C language. Void can be used as an lvalue to accept any type of pointer,
However, when void
used as an rvalue,must be forced type conversion.
// test.cpp int main() { void* pv = nullptr; // 初始化是个好习惯 int* pi = pv; // error, invalid conversion from void* to int* int* pi = (int*)pv; // ok, due to cast return 0; }
Summary: void* pointer is to receive pointers of any type.
Example Demo: Set all the memory occupied by the array to 0
void MemSet(void* src, int length, unsigned char n)
{
unsigned char* p = (unsigned char*)src;
int i = 0;
for(i=0; i<length; i++)
{
p[i] = n;
}
}
int main()
{
int a[5];
int i = 0;
MemSet(a, sizeof(a), 0);
for(i=0; i<5; i++)
{
printf("%d\n", a[i]);
}
return 0;
}
This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。