Global Char Pointer (C programming)

I am just starting to learn the C language by creating small programs.

Now I am in the middle of creating a program that requires either a structure 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 am afraid that this will increase the likelihood of seg. an error somewhere between the 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 someone tell me the correct way to create a global char pointer without creating me a structure?

+4
source share
2 answers

You should use structif variables make up an object. If they:

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

. myGlobalVar - struct myTrialStruct.

, , :

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

struct segfault; .

. , .

+4

, .

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

, ,

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

-

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

, .

?

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

, , .

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

, ,

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

struct myTrialStruct hlobal_pointers;
+4

Source: https://habr.com/ru/post/1613745/


All Articles