so I understand that global variables are evil, and programmers should avoid them. However, I have been doing some studying regarding potential vulnerabilities behind it, so that is the reason why I'm using it at the moment.
I have this toy example:
#define MAX_LENGTH 8
int counter = MAX_LENGTH;
int *size;
int main(int argc, char **argv)
{
int value = atoi(argv[1]);
size = &value;
int *test;
*test = value;
printf("Hello World %d %d\n", *size, *test);
}
So the core question is that why for a global variable pointer, do I have to get the address of a variable to store the value stored in that variable?
My understanding of storing a value to a pointer is how I used the int *test;
variable. I simply dereference this variable *test = value
to store the value. However, I am curious why for global variable pointer *size
, I cannot simply do *size = value
, but instead must do size = &value
.
I tried searching this question through StackOverflow, but maybe I didn't phrase the question right or such, so I could not find the relevant link for it.
I would appreciate any kind of tips, thank you in advance,
Kind regards,
Edit: Thanks everyone for the responses!