Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a pointer pointing to a static variable, and a static pointer pointing to some variable

Tags:

c++

c

Is there any difference between

static int * pn;

and

int static * pn;

Basically I am looking for difference between a pointer pointing to a static variable, and a static pointer pointing to some variable, and not sure whether the above 2 declarations corresponds to them.

like image 835
Hailiang Zhang Avatar asked Dec 18 '12 22:12

Hailiang Zhang


People also ask

What is the difference between a pointer and a pointer variable?

a pointer is nothing more than an address, and a pointer variable is just a variable that can store an address. When we store the address of a variable i in the pointer variable p , we say that p points to i . int i, *p = &i; p points to i .

Can we use this pointer for the static variable?

So, THIS pointer is not passed to a static function as an internal parameter. So, a static function does not understand THIS pointer inside its body. If we compile below class, compiler with throw an error i.e. static functions do not have this pointer.

What is a static pointer?

A static pointer can be used to implement a function that always returns the same buffer to the program. This can be helpful in serial communication.

What's the difference between a pointer and a reference?

References are used to refer an existing variable in another name whereas pointers are used to store address of variable. References cannot have a null value assigned but pointer can. A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.


2 Answers

In C there is no difference but the second form is discouraged.

(C99, 6.11.5p1) "The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature"

like image 65
ouah Avatar answered Nov 14 '22 21:11

ouah


No, there is no difference. Here, the declaration specifier sequence is static int or int static and the order of specifiers in a declaration specifier sequence doesn't matter. Both of your declarations have type "pointer to int".

You can do other weird stuff like int static unsigned const* pn; if you hate people enough. I wrote a question/answer that covers this topic.

like image 44
Joseph Mansfield Avatar answered Nov 14 '22 21:11

Joseph Mansfield