简体   繁体   English

C中的结构声明和定义

[英]Structure declaration and definition in C

If we write something like this: 如果我们这样写:

typedef struct 
{
    unsigned int counter;
} MyStruct;

This represents structure declaration? 这代表结构声明吗?

Declaration says our compiler that somewhere there is a structure which have parameters of type and size like above, and no space is reserved in memory for that structure in case of declaration. 声明说我们的编译器说,在某处有一个结构,该结构具有上面类型和大小的参数,在声明的情况下,该结构在内存中没有保留空间。

and definition is, as now we reserved a space in memory for our structure: 定义是,因为现在我们为结构保留了一个内存空间:

MyStruct tmpStruct;

Or I am wrong? 还是我错了?

Please clarify the situation with structures. 请用结构说明情况。

There are different kinds of definitions in C - type definitions, variable definitions, and function definitions. C语言中有不同种类的定义-类型定义,变量定义和函数定义。 Types and functions could also be declared without being defined. 类型和函数也可以在不定义的情况下声明。

Your typedef is not a declaration, it's a definition of a type . 您的typedef不是声明,而是type的定义。 In addition to defining a struct with no tag, it defines a type name that corresponds to that struct . 除了定义不带标签的struct ,它还定义与该struct相对应的类型名称。

A declaration of a struct would look like this: struct的声明如下所示:

typedef struct MyStruct MyStruct;

This would let you declare pointers to MyStruct , forward-declare functions that take MyStruct* , and so on: 这将使您能够声明指向MyStruct指针,采用MyStruct*前向声明函数,等等:

void foo(MyStruct* p);

What you have is a declaration/definition of a variable of type MyStruct : 您所拥有的是MyStruct类型的变量的声明/定义:

MyStruct tmpStruct;

Here is a complete example with struct 's declaration separated from its definition: 这是一个完整的示例,其中struct的声明与其定义分开:

#include <stdio.h>
// Here is a declaration
typedef struct MyStruct MyStruct;
// A declaration lets us reference MyStruct's by pointer:
void foo(MyStruct* s);
// Here is the definition
struct MyStruct {
    int a;
    int b;
};
// Definition lets us use struct's members
void foo(MyStruct *p) {
    printf("%d %d\n", p->a, p->b);
}

int main(void) {
    // This declares a variable of type MyStruct
    MyStruct ms = {a:100, b:120};
    foo(&ms);
    return 0;
}

Demo. 演示。

There is a great write-up on this over here 这里有很多很棒的文章

https://www.microforum.cc/blogs/entry/21-how-to-struct-lessons-on-structures-in-c/ https://www.microforum.cc/blogs/entry/21-how-to-struct-lessons-on-structures-in-c/

The first thing you declared up there is a type declaration combined with a structure declaration, no definition so no space allocated correct. 您首先声明的是类型声明和结构声明,没有定义,因此没有分配正确的空间。

The proper way to declare a struct is to say : 声明结构的正确方法是说:

   struct MyStruct
   {
      unsigned int counter; 
   };

You can now do a definition of an instance like this: 现在,您可以这样定义一个实例:

   struct Mystruct myVariableStructInstance = {1234};  // Define struct instance with initializer

It is also possible to combine the declaration with a definition like this : 也可以将声明与这样的定义结合起来:

   struct MyStruct_t
   {
      unsigned int counter; 
   }  MyStructVariable;

This will declare the structure type (struct MyStruct_t) and also define/instantiate a struct variable for you named MyStructVariable 这将声明结构类型(struct MyStruct_t),并为您定义/实例化名为MyStructVariable的struct变量

You can now pass MyStructVariable into a function declared thus: 您现在可以将MyStructVariable传递到这样声明的函数中:

void myFunction(struct MyStruct_t  temp);

Which you will call simply with 您将简单地称之为

myFunction(MyStructVariable);

But really, read that blog post, it explains a lot more than just this! 但是,实际上,请阅读该博客文章,它不仅解释了很多内容!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM