简体   繁体   English

全局字符指针(C编程)

[英]Global Char Pointer (C Programming)

I'm just getting started to learn C language by creating small programs. 我刚刚开始通过创建小程序来学习C语言。

Now, I'm in the middle of creating a program that require either struct or something global, because later on somewhere in my function I just want to call them directly. 现在,我正在创建一个需要结构或全局的程序,因为稍后我的函数中的某个地方我只想直接调用它们。

I prefer not to use struct because I'm afraid that it will increase the chance of seg. 我不喜欢使用struct,因为我担心它会增加seg的机会。 fault somewhere between lines of code. 代码行之间的某种错误。

My question is: Are these two the same? 我的问题是:这两个是一样的吗?

struct myTrialStruct{
    char *trialOne;
    char *trialTwo;
    char *trialThree;
};

and

extern char *trialOne;
extern char *trialTwo;
extern char *trialThree;

char *trialOne;
char *trialTwo;
char *trialThree;

If not, can somebody tell me the proper way to create a global char pointer without having me to create a struct ? 如果没有,有人可以告诉我创建全局字符指针的正确方法,而无需创建结构吗?

You should use a struct , if the variables make up an object. 如果变量构成对象,则应使用struct If they do: 如果他们这样做:

struct myTrialStruct{
    char *trialOne;
    char *trialTwo;
    char *trialThree;
} myGlobalVar;

Just write this at the top of your source file. 只需将其写在源文件的顶部即可。 myGlobalVar is a global variable of type struct myTrialStruct . myGlobalVarstruct myTrialStruct类型的全局变量。

Or if the three variables are part of a sequence use an array: 或者,如果三个变量是序列的一部分,请使用数组:

char *myGlobalArr[3];  // array of char *

Using a struct won't increase the chance of segfault; 使用struct不会增加段错误的可能性; careless pointers will. 粗心的指针会。

And try to limit the use of global variables. 并尝试限制全局变量的使用。 Your code will become very disorganized, and it will be harder to fix bugs. 您的代码将变得非常混乱,修复错误将更加困难。

If you have one compilation unit then it is enough to place these declarations outside any function. 如果您有一个编译单元,那么将这些声明放在任何函数之外就足够了。

char *trialOne;
char *trialTwo;
char *trialThree;

If you are going to have several compilation units that will access these pointers then you should to place these declarations in a header 如果您要使用几个可以访问这些指针的编译单元,那么您应该将这些声明放在标题中

extern char *trialOne;
extern char *trialTwo;
extern char *trialThree;

and in some module to place these definitions of the pointers 并在某些模块中放置指针的这些定义

char *trialOne;
char *trialTwo;
char *trialThree;

The header should be included in any compilation unit that need to access these pointers. 标头应包含在需要访问这些指针的任何编译单元中。

As for your question 至于你的问题

Are these two the same? 这两个是一样的吗?

then these 然后这些

struct myTrialStruct{
    char *trialOne;
    char *trialTwo;
    char *trialThree;
};

a type declaration that is it is a structure declaration, no object is created. 一个类型声明,它是一个结构声明,没有创建对象。

while these are object declarations 而这些是对象声明

char *trialOne;
char *trialTwo;
char *trialThree;

You could define an object of the structure type for example the folloiwng way 您可以定义结构类型的对象,例如folloiwng方式

struct myTrialStruct{
    char *trialOne;
    char *trialTwo;
    char *trialThree;
};

struct myTrialStruct hlobal_pointers;

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

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