开发者

Can we differentiate a variable against a pointer variable

开发者 https://www.devze.com 2023-04-08 23:44 出处:网络
Yesterday while I was coding in C, my friend asked me pointing to a variable is it pointer or a variable ? I stucked up for a while. I didnt find an aswer to it , I just have to go back and search it

Yesterday while I was coding in C, my friend asked me pointing to a variable is it pointer or a variable ? I stucked up for a while. I didnt find an aswer to it , I just have to go back and search it and tell him.But I was thinking is there any function to differentiate them. Can we differentiate a variable against a pointer variable

int a;
sizeof(a); // gives 2 bytes
int *b;
sizeof(b); // gives 2 bytes
// if we use sizeof() we get same answer and we cant say which is pointer
// and which is a variable

Is there a way to find out a variable is a normal variable or a pointer? I mean can someone say that it is a pointer or a variable after looking at your variable that you ha开发者_如何学Pythonve declared at the beginning and then going down 1000 lines of your code?

After the comment I wanted to say explicitly it's a 16 bit system architecture.


First, the question "Is it a pointer or a variable" doesn't make much sense. A pointer variable is a variable, just as an integer variable, or an array variable, is a variable.

So the real question is whether something is a pointer or not.

No, there's no function that can tell you whether something is a pointer or not. And if you think about it, in a statically typed language like C, there can't be. Functions take arguments of certain specified types. You can't pass a variable to a function unless the type (pointer or otherwise) is correct in the first place.


You mean differentiate them at run time without seeing the code? No, you can't. Pointers are variables that hold memory address. You can't check it at run time. That means, there is no such function isPointer(n) that will return true/false based on parameter n.


You can deduce the type from the use.

For example:

char* c;
...
c[0] = 'a';
*c = 'a';

Indexing and dereferencing would let you know it's a pointer to something (or it's an array if defined as char c[SOME_POSITIVE_NUMBER];). Also, things like memset(c,...), memcpy(c,...) will suggest that c is a pointer (array).

OTOH, you can't normally do with pointers most of arithmetic, so, if you see something like

x = c * 2;
y = 3 / c;
z = c << 1;
w = 1 & c;

then c is not a pointer (array).


Three things:

  1. What platform are you using where sizeof(int) returns 2? Seriously
  2. Pointers are types. A pointer to an int is a type, just like an int is. The sizes of a type and a pointer to that type are sometimes equal but not directly related; for instance, a pointer to a double (on my machine, at least) has size 4 bytes while a double has size 8 bytes. sizeof() would be a very poor test, even if there was a situation where such a test would be appropriate (there isn't).
  3. C is a strictly typed language, and your question doesn't really make sense in that context. As the programmer, you know exactly what a is and you will use it as such.


If you'd like to be able to tell whether a variable is a pointer or not when you see it in the source code, but without going back to look at the declaration, a common approach is to indicate it in the way you name your variables. For example, you might put a 'p' at the beginning of the names of pointers:

int *pValue; /* starts with 'p' for 'pointer' */
int iOther;  /* 'i' for 'integer' */

...or even:

int *piSomething; /* 'pi' for 'Pointer to Integer' */

This makes it easy to tell the types when you see the variable in your code. Some people use quite a range of prefixes, to distinguish quite a range of types.

Try looking up "Hungarian notation" for examples.


no , you can't. and what is the usage, as each time u run the code the pointer address will be different ?? however u can subtract two pointers and also can get the memory address value of any pointer.

0

精彩评论

暂无评论...
验证码 换一张
取 消