如何将局部结构传递给函数?
How to pass local structures to functions?
下面的 link 表示 main
中定义的结构没有函数调用的范围,因为它们是局部的,所以您应该在全局范围内定义您的结构。但是对于变量,最好在本地声明变量并使用指针传递给函数,而不是声明全局变量。
有没有办法在纯 C 中使用指针等将 main 中定义的结构传递给函数?如果您不介意,请使用示例程序来演示该方法。谢谢。
where to declare structures, inside main() or outside main()?
此代码有效,但不是我想要的。我想在 main
内定义结构。这可能吗?
#include <stdio.h>
#include <SDL2/SDL.h>
void function();
struct hexColour
{
Uint32 red;
}hc;
int main(void)
{
hc.red = 0xFFFF0000;
function(hc);
return 0;
}
void function(struct hexColour hc)
{
printf("red is %x\n", hc.red);
}
我想要的是:
int main(void)
{
struct hexColour
{
Uint32 red;
}hc;
hc.red = 0xFFFF0000;
function(hc);
return 0;
}
首先,您应该真正使用与函数定义相匹配的适当原型。
其次,您的示例 do 将结构传递给函数中的局部变量 hc
。
当function
为运行 时,内存中有两个截然不同的独立结构:一个在main
函数中,另一个在function
函数中。
为了涵盖我的基础,这里有两个可能被问到的另外两个问题的答案:
您想在 main
函数中定义结构本身,然后可以在其他函数中使用它。
类似
int main(void)
{
struct hexColor
{
uint32_t white;
// Other members omitted
};
struct hexColour hc;
hc.white = 0xff;
func(hc); // Assume declaration exist
}
void func(struct hexColour my_colour)
{
printf("White is %u\n", my_colour.white);
}
这不可能。结构 hexColour
仅在 main
函数内部定义。没有其他功能可以使用该结构。是否传递指针并不重要,结构 hexColour
仍然只存在于 main
函数中。
通过将指针传递给结构对象来模拟按引用传递。喜欢
struct hexColor
{
uint32_t white;
// Other members omitted
};
int main(void)
{
struct hexColour hc;
hc.white = 0xff;
// Assume declaration of function exists
func(&hc); // Emulate pass-by-reference by passing a pointer to a variable
}
void func(struct hexColour *colourPointer)
{
colourPointer->white = 0x00;
}
这个是可能的,因为结构hexColour
存在于main
函数之外,在全局范围内。在结构定义之后声明和定义的所有函数都可以使用该结构及其成员。
如果按值传递,则复制一份(昂贵,修改不会反映在外面)。如果你想传递一个指向 struct
的指针,那么 但这并不意味着你通过引用传递 struct
(C 没有引用),你正在按值而不是 .
传递指向 struct
的指针
void function(struct hexColour* hc) {
printf("red is %x", hc->red);
}
int main() {
...
functon(&hc);
...
}
参见:
- 函数的签名从
struct hexColor
更改为 struct hexColor*
,以便您传递指针(按值)
- 在处理指针时要访问
struct
的字段,您使用 ->
而不是 .
- 调用函数时需要取struct的地址,
function(hc)
变为function(&hc)
现在,由于您正在传递结构的地址,因此对实际值进行了任何修改。
您似乎没有完全理解您的链接问题及其答案。你写,
The following link says that structs defined in main don't have the
scope to be called by functions because they are local so you should
define your structs globally.
这里的歧义是在结构 types 之间,比如你的 struct hexColour
和 objects 有那些类型,例如您的 hc
。 struct types 和 struct objects 都应该被声明,以便它们在所有需要它们的地方都在范围内,但是对于这两种不同类型的实体和在各种不同的情况下,这会有不同的表现。
However with variables, it's preferred to declare variables locally
and pass by reference instead of declaring global variables.
通常最好使用block-scope变量而不是file-scope变量,是的,但是C只有按值传递,没有按引用传递。在很多情况下,传递指针(按值)而不是它们指向的 objects 是有利的,这接近于按引用传递,但肯定没有规则或一般做法传递指针普遍比传递它们指向的 objects 更好。
Is there
a way in pure C using pointers etc to pass a local struct to a
function?
调用者和被调用者都必须就每个参数的类型达成一致,有很多方法可以实现这一点。但是随着 C 的发展,有一些约定可以有效地解决诸如此类的问题。其中很大一部分是:
- 要在多个翻译单元中使用的任何函数和任何 non-builtin 类型都应在 header 文件中声明,并且 header 包含在需要的每个翻译单元中它。
这是您根据 "global" 定义制定的规则的概括。示例:
colour.h
#ifndef COLOUR_H
#define COLOUR_H
struct hexColour {
Uint32 white;
Uint32 black;
Uint32 red;
Uint32 pink;
Uint32 grey;
}; // Note that only the type is declared here, not any objects
void function(struct hexColour hc);
#endif
请注意,类型 struct hexColour
的声明出现在具有该类型参数的函数 function
的声明之前。
然后,您可以在适当的位置使用这些类型和函数,例如:
main.c:
#include "colour.h"
int main(void) {
struct hexColour hc = {
.white = 0xFFFFFFFF, .black = 0xFF000000, .red = 0xFFFF0000,
.pink = 0xFFFF9999, .grey = 0xFFA0A0A0 };
function(hc);
return 0;
}
void function(struct hexColour hc) {
printf("red is %x\n", hc.red);
}
请注意,构成其定义一部分的 function
的声明与 header 中的声明相匹配。该定义 function()
可以很容易地在不同的源文件中定义,只要调用者有 header 文件来告诉它该函数是如何声明的。您可以根据需要 #include
coulour.h 放入任意数量的不同源文件。
但是请注意,在这种情况下,结构是按值传递的。这是 well-defined 并且完全可以接受,但是由于该函数仅接收一个副本,因此它无法影响对调用者原始副本的更改。如果你希望函数能够做到这一点,那么你需要传递一个指向结构的指针(按值)而不是结构本身:
void function(struct hexColour *hc) {
// ...
}
int main(void) {
// ...
function(&hc);
// ...
}
您可以采用本地定义的结构并将其传递给另一个函数:
void f1(struct s);
int main()
{
struct s s1;
f1(s1);
}
您可以采用本地定义的结构并将指向它的指针传递给另一个函数:
void f2(struct s *);
int main()
{
struct s s2;
f2(&s2);
}
您可以 return 本地定义的结构:
struct s f3()
{
struct s ret;
/* ... */
return ret;
}
您可以不 return 指向本地定义结构的指针:
struct s *f4()
{
struct s ret;
/* ... */
return &ret; /* WRONG */
}
如果你在一个函数中声明一个结构,它不是一个全局变量,所以你不能在你没有传递结构或指针的另一个函数中引用它:
void f5();
int main()
{
struct s s1;
f5();
}
void f5()
{
int x = s1.field; /* WRONG */
}
最后,如果您在函数内部声明结构类型本身,您最终会得到一个无法在别处正确引用的结构:
void f1(struct s);
int main()
{
struct s { int a, b; } s1;
f1(s1);
}
void f1(struct s arg) /* error: this struct s might not be the same as the previous one */
{
int x = arg.b; /* error: compiler might not know that struct s contains a and b */
}
看了之前的回答。建议您仔细考虑声明和定义之间的 'C' 区别。请注意,'C' 的早期版本不允许在堆栈上传递结构的副本,只允许传递指向结构的指针..
下面的 link 表示 main
中定义的结构没有函数调用的范围,因为它们是局部的,所以您应该在全局范围内定义您的结构。但是对于变量,最好在本地声明变量并使用指针传递给函数,而不是声明全局变量。
有没有办法在纯 C 中使用指针等将 main 中定义的结构传递给函数?如果您不介意,请使用示例程序来演示该方法。谢谢。
where to declare structures, inside main() or outside main()?
此代码有效,但不是我想要的。我想在 main
内定义结构。这可能吗?
#include <stdio.h>
#include <SDL2/SDL.h>
void function();
struct hexColour
{
Uint32 red;
}hc;
int main(void)
{
hc.red = 0xFFFF0000;
function(hc);
return 0;
}
void function(struct hexColour hc)
{
printf("red is %x\n", hc.red);
}
我想要的是:
int main(void)
{
struct hexColour
{
Uint32 red;
}hc;
hc.red = 0xFFFF0000;
function(hc);
return 0;
}
首先,您应该真正使用与函数定义相匹配的适当原型。
其次,您的示例 do 将结构传递给函数中的局部变量 hc
。
当function
为运行 时,内存中有两个截然不同的独立结构:一个在main
函数中,另一个在function
函数中。
为了涵盖我的基础,这里有两个可能被问到的另外两个问题的答案:
您想在
main
函数中定义结构本身,然后可以在其他函数中使用它。类似
int main(void) { struct hexColor { uint32_t white; // Other members omitted }; struct hexColour hc; hc.white = 0xff; func(hc); // Assume declaration exist } void func(struct hexColour my_colour) { printf("White is %u\n", my_colour.white); }
这不可能。结构
hexColour
仅在main
函数内部定义。没有其他功能可以使用该结构。是否传递指针并不重要,结构hexColour
仍然只存在于main
函数中。通过将指针传递给结构对象来模拟按引用传递。喜欢
struct hexColor { uint32_t white; // Other members omitted }; int main(void) { struct hexColour hc; hc.white = 0xff; // Assume declaration of function exists func(&hc); // Emulate pass-by-reference by passing a pointer to a variable } void func(struct hexColour *colourPointer) { colourPointer->white = 0x00; }
这个是可能的,因为结构
hexColour
存在于main
函数之外,在全局范围内。在结构定义之后声明和定义的所有函数都可以使用该结构及其成员。
如果按值传递,则复制一份(昂贵,修改不会反映在外面)。如果你想传递一个指向 struct
的指针,那么 但这并不意味着你通过引用传递 struct
(C 没有引用),你正在按值而不是 .
struct
的指针
void function(struct hexColour* hc) {
printf("red is %x", hc->red);
}
int main() {
...
functon(&hc);
...
}
参见:
- 函数的签名从
struct hexColor
更改为struct hexColor*
,以便您传递指针(按值) - 在处理指针时要访问
struct
的字段,您使用->
而不是.
- 调用函数时需要取struct的地址,
function(hc)
变为function(&hc)
现在,由于您正在传递结构的地址,因此对实际值进行了任何修改。
您似乎没有完全理解您的链接问题及其答案。你写,
The following link says that structs defined in main don't have the scope to be called by functions because they are local so you should define your structs globally.
这里的歧义是在结构 types 之间,比如你的 struct hexColour
和 objects 有那些类型,例如您的 hc
。 struct types 和 struct objects 都应该被声明,以便它们在所有需要它们的地方都在范围内,但是对于这两种不同类型的实体和在各种不同的情况下,这会有不同的表现。
However with variables, it's preferred to declare variables locally and pass by reference instead of declaring global variables.
通常最好使用block-scope变量而不是file-scope变量,是的,但是C只有按值传递,没有按引用传递。在很多情况下,传递指针(按值)而不是它们指向的 objects 是有利的,这接近于按引用传递,但肯定没有规则或一般做法传递指针普遍比传递它们指向的 objects 更好。
Is there a way in pure C using pointers etc to pass a local struct to a function?
调用者和被调用者都必须就每个参数的类型达成一致,有很多方法可以实现这一点。但是随着 C 的发展,有一些约定可以有效地解决诸如此类的问题。其中很大一部分是:
- 要在多个翻译单元中使用的任何函数和任何 non-builtin 类型都应在 header 文件中声明,并且 header 包含在需要的每个翻译单元中它。
这是您根据 "global" 定义制定的规则的概括。示例:
colour.h
#ifndef COLOUR_H
#define COLOUR_H
struct hexColour {
Uint32 white;
Uint32 black;
Uint32 red;
Uint32 pink;
Uint32 grey;
}; // Note that only the type is declared here, not any objects
void function(struct hexColour hc);
#endif
请注意,类型 struct hexColour
的声明出现在具有该类型参数的函数 function
的声明之前。
然后,您可以在适当的位置使用这些类型和函数,例如:
main.c:
#include "colour.h"
int main(void) {
struct hexColour hc = {
.white = 0xFFFFFFFF, .black = 0xFF000000, .red = 0xFFFF0000,
.pink = 0xFFFF9999, .grey = 0xFFA0A0A0 };
function(hc);
return 0;
}
void function(struct hexColour hc) {
printf("red is %x\n", hc.red);
}
请注意,构成其定义一部分的 function
的声明与 header 中的声明相匹配。该定义 function()
可以很容易地在不同的源文件中定义,只要调用者有 header 文件来告诉它该函数是如何声明的。您可以根据需要 #include
coulour.h 放入任意数量的不同源文件。
但是请注意,在这种情况下,结构是按值传递的。这是 well-defined 并且完全可以接受,但是由于该函数仅接收一个副本,因此它无法影响对调用者原始副本的更改。如果你希望函数能够做到这一点,那么你需要传递一个指向结构的指针(按值)而不是结构本身:
void function(struct hexColour *hc) {
// ...
}
int main(void) {
// ...
function(&hc);
// ...
}
您可以采用本地定义的结构并将其传递给另一个函数:
void f1(struct s);
int main()
{
struct s s1;
f1(s1);
}
您可以采用本地定义的结构并将指向它的指针传递给另一个函数:
void f2(struct s *);
int main()
{
struct s s2;
f2(&s2);
}
您可以 return 本地定义的结构:
struct s f3()
{
struct s ret;
/* ... */
return ret;
}
您可以不 return 指向本地定义结构的指针:
struct s *f4()
{
struct s ret;
/* ... */
return &ret; /* WRONG */
}
如果你在一个函数中声明一个结构,它不是一个全局变量,所以你不能在你没有传递结构或指针的另一个函数中引用它:
void f5();
int main()
{
struct s s1;
f5();
}
void f5()
{
int x = s1.field; /* WRONG */
}
最后,如果您在函数内部声明结构类型本身,您最终会得到一个无法在别处正确引用的结构:
void f1(struct s);
int main()
{
struct s { int a, b; } s1;
f1(s1);
}
void f1(struct s arg) /* error: this struct s might not be the same as the previous one */
{
int x = arg.b; /* error: compiler might not know that struct s contains a and b */
}
看了之前的回答。建议您仔细考虑声明和定义之间的 'C' 区别。请注意,'C' 的早期版本不允许在堆栈上传递结构的副本,只允许传递指向结构的指针..