Is accessing the static value using a global pointer variable is valid?

global variables in c
difference between static local and static global variable in c
extern in c
static variable in c++
default value of local variable in c
how to access global variable in c++
c++ global variable in header
static variable vs global variable

Can I access the static variable using the global pointer?

//In main.c file
static int c = 20; #Static vairble 
int *ptr = &c; #Global pointer variable
int main(){
  printf("%d\n", c);

//In sub.c file
extern int *ptr;
void fun(){

When I tried to access the *ptr global variable using the extern keyword in another file I'm able to access the static variable c and even the changes made on the *ptr is reflecting on c in main.c file

  1. Why does "ptr" not loses its global property even after its assigned by a static variable address?

  2. Does static and global are stored in two different segments of memory or they both share the same data segment of memory?

  3. Can global variables store non-constant values?

You're confusing the linkage of a pointer variable and the linkage of what it points to.

The linkage of a pointer does not change, regardless of what it points to.

Here the linkage of ptr is global (aka external) and the linkage of c is static (aka internal). Neither one changes the linkage of the other.

static means that c will not be visible (i.e. available for direct linkage) in another (e.g.) .c file. That is, another .c could not do:

int *ptr2 = &c;

But, it could do:

int **ptr3 = &ptr;

You could have the reverse:

int c = 20;
static int ptr = &c;

This is a static/internal linkage ptr pointing to a global/external linkage c.

A program has several memory areas: .text for code. .data for initialized variables (such as c) and .bss for unitialized areas.

Since you initialize both ptr and c, they both end up in .data

If you had done:

#include <stdio.h>

static int c = 20; // Static vairble
int *ptr;

int main(){
    ptr = &c;
    return 0;

Then, c is initialized and goes in .data but ptr is not initialized but is assigned and goes in the .bss segment

All variables [global or not] can have non-constant values. Either by initialization or assignment.

All above variables have global/file scope.

Here is another type of scope:

#include <stdio.h>

static int c = 20; // Static vairble

int main(){
    int *ptr = &c;
    return 0;

Here, ptr has function scope. It is placed on the stack frame of main. It has no linkage to speak of (i.e. it will never show up in the symbol table of the executable).

Access a global static variable from another file in C, How do I view a static global variable in another file? In line 4, a and b are declared as two global variables of type int.The variable a will be automatically initialized to 0. You can use variables a and b inside any function. . Notice that inside function func_2() there is a local variable with the same name as a global variab

static is not about laying the object into a portion of memory that cannot be altered /addressed from other translation units. It is just about defining that the object has internal linkage and therefore cannot be grabbed by other translation units through this (internal) name.

But if you somehow get the memory address of an object declared static in another translation unit, you may legally access the object.

So your construct is valid.

One might just obey the "static initialization order fiasco" when other translation units draw copies of your ptr (probably before this got pointed to c). Consider, for example, what might happen when another translation unit contains at file scope...

extern int *ptr;
int *myPtr = ptr;
int main() {
   int x = *myPtr;

What is the difference between a static variable and global variable , What is the use of global static variable in C? (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. The following example makes use of these operations −

Why does "ptr" not loses its global property even after its assigned by a static variable address?

A variable cannot "loose" its scope or storage class, the static linkage class specifier determines the visibility of the symbol, not where or how it is stored. It prevents the data being referenced via that symbol, it does not prevent pointer references to the same location. C is a systems level language - as such it does not prevent you from doing pretty much anything that can be done at architecture level.

Does static and global are stored in two different segments of memory or they both share the same data segment of memory?

No. The static keyword is used in this case to specify static or or internal linkage, it is also used (for function-local variables) as a storage class specifier, but all variables outside a function have static storage class regardless of internal or external linkage.

That said, initialised static storage data is placed in a different linker section than zero or uninitialised data. This is to facilitate runtime start-up initialisation.

Can global variables store non-constant values?

Perhaps that deserves a separate question to explain why you might think otherwise - it seems unrelated to your other questions and a but left-field. In your own example ptr is storing &c, but that could be modified at run-time to point elsewhere.

What's the difference between Pointers and Global Variables in C , Home » C Programming Tutorial » Local, Global and Static variables in C If you try to use these variables outside the function in which they are defined, you will If you change the value of a inside the function_1() then it will not change the Returning more than one value from function in C · Returning a Pointer from a  We calculate the cube value of the content variable (x) pointed by the num pointer, and as it is a void pointer, we have to type cast it to an integer data type using a specific notation (* datatype) pointer, and we return the cube value. We declare the operand and the result variable. Also, we initialize our operand with value "4."

Why does "ptr" not loses its global property even after its assigned by a static variable address?

ptr remains global regardless where it points since it is declared global.

Does static and global are stored in two different segments of memory or they both share the same data segment of memory?

In general they reside in the same data segment.

Can global variables store non-constant values?

yes, just like any other variable, thus the name "variable"

Global Variables, extern, static, const, 3) Static variables (like global variables) are initialized as 0 if not initialized explicitly. 4) In C, static variables can only be initialized using constant literals. The above method of accessing members of the structure using pointers is slightly confusing and less readable, that’s why C provides another way to access members using the arrow (->) operator. To access members using arrow ( -> ) operator write pointer variable followed by -> operator, followed by name of the member.

Local, Global and Static variables in C, As with all constants, constant global variables must be initialized. const int g_y { 1 }; // const global variable g_y, initialized with a value Saying that this is a declaration is correct, but very confusing. This is a case where the finance.cpp and students.cpp are different, but need access to a single 'secret' variable. If it is a variable, it must have a valid C data type. Yes, every pointer variable has a data type associated with it. Which means an integer pointer can hold only integer variable addresses. Note: We never say pointer stores or holds a memory location.

Static Variables in C, If the static member variables are public, we can access them directly using the While we could create a normal public member function to access s_value, we'd then member functions are not attached to an object, they have no this pointer! change the value of the global variable and end up breaking another piece of  A Global Static variable serves no useful purpose as it is essentially the same as a normal Global variable. However, a Local Static variable exists only within the function in which it is declared, but is NOT destroyed when the function ends and can be reused when the function is next entered.

6.4, It can only be accessed by functions in that file, similar to how a private static The only issue is unlike with a true private static variable as in C++, other files can model is slanted towards virtual dispatch and function pointers and so on. A compromise is to separate calculating the initial value from the static variable​,  When an integer is subtracted from a pointer variable, the value of the pointer variable is decremented by ____. A: the integer times the size of the memory to which the pointer is pointing B: the integer C: the size of the memory to which the pointer is pointing D: the integer plus the size of the memory to which the pointer is pointing

  • What does ++*ptr actually do? What indirection is occuring?
  • Scope is not the issue. Both c and ptr have file scope (which is the term used by the C standard, not global or static). The issue is linkage, which is the process by which the same identifier in different scopes can be made to refer to the same object. c has internal linkage. ptr has external linkage.