Home > Back-end >  Local and global variables in the c language pointer problem??
Local and global variables in the c language pointer problem??

Time:09-21

#include

Int * PI;
Short gi=12;

Void main ()
{
PI=(int *) & amp; Gi;

Printf (" * PI=% d, % * PI=x \ n ", * PI, * PI);
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include

Void main ()
{
Int * PI;
Short gi=12;
PI=(int *) & amp; Gi;

Printf (" * PI=% d, % * PI=x \ n ", * PI, * PI);
}

The results of the two pieces of code why so big difference, and address global variable and a local variable to store data have any relationship, and locally defined time can't see the memory address, use the VS2005 compiler

CodePudding user response:

What is the result

CodePudding user response:

The result should be the same

CodePudding user response:

The result should be different,,,

CodePudding user response:

Short definition in a local variable, two bytes, when the cursor is extended to 4 bytes, the first two bytes (2 bytes) after memory should be content may be unknown.
Short definition in a global variable, is 2 bytes, but a global variable is aligned and so initialization is initialized at 4 byte, so after extends the read also didn't go wrong.
  • Related