current position:Home>How to define and initialize structures in. H

How to define and initialize structures in. H

2022-02-02 23:04:29 CSDN Q & A

To facilitate development , I defined a Global.h The file of , And in this file , Defines a structure Stu, In order to use , I defined another EmptyStu, And copy it , As the code shows :

#ifndef _GLOBAL_H_#define _GLOBAL_H_struct Stu{    char * name ;    char * info ;    long age ;}typedef struct Stu Stu ;const Stu EmptyStu = {
   
   
   
   NULL,NULL,0}#endif

It's such a program , The result is wrong , Yes 2 A mistake :

 Main.c:(.bss+0x0):multiple definition of `EmptyStu'Global.c:(.bss+0x0): first defined here LoginUI.c:(.bss+0x0): multiple definition of `EmptyStu'Global.c:(.bss+0x0): first defined here

Be careful , I'm here Main.c and LoginUI.c It's all imported "Global.h".

put questions to :
1、 I can't figure out why the definition is repeated ;
2、 stay .h In file , How to define the structure , And give a fixed value to one of the variables , And it's best to keep its global variable characteristics , as well as const Make sure it doesn't change ?




Refer to the answer 1:

Add a constructor to the structure to initialize the default value




Refer to the answer 2:



Refer to the answer 3:

thank you , I've solved it myself




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202022304269499.html

Random recommended