A pointer variable is a variable that contains the memory location of another variable or an array (or anything else in memory). Effectively, it points to another memory location.
For standard variables, you define a type, assign a value to that variable or read the value from it, and you can also read the memory location (&n = memory location of n) of the variable.
For pointers, you can point them to any variable, even another pointer, and you can get the value of the variable it points to (*p), the location of that variable in memory (p), or the address in memory of the pointer itself (&p).
Consider:
long n = 65;
long *p;
p = &n;
Results:
Type | Name | Location | Value
long n 0xA3FF 65
long * p 0x31DF 0xA3FF
So p points to n. Now,
n = 65
&n = 0xA3FF
p = 0xA3FF
*p = 65
&p = 0x31DF
You may find yourself having to use typecasts frequently when using pointers.
Pointers are useful when passing data to functions.
For instance, consider the following function:
void add(int a, int b, int c) { c = a + b; }
The problem here is that the computer copies each variable into a new memory location before passing them to the function as local variables. This function effectively does nothing. However, if you change the function to:
void add(int a, int b, int *c) { c = a + b; }
and call the function by passing in the location of the variable to the function:
add(a,b,&c);
then you can modify the variable itself.
Pointers are also good for working with arrays:
char *c = "Hello World";
int i=0;
while (c[i] != 0x00) { cout << c[i]; c++ } //print one letter at a time.
A pointer type differs from all other types in that it is used specifically for the purpose of storing a memory address and for permitting indirect access to the value stored at that memory address. When we store a memory address, we are said to be pointing at that memory address. When we indirectly access the value stored at that memory address, we are said to be dereferencing the pointer. We can also say that a pointer holds a reference to the object stored at the memory address it points to.
Pointers may only store memory addresses where the memory holds a value of the same type as the pointer, or is of a type that is co-variant with the pointer's type. Where the type is unknown we can use a void pointer, however this is best avoided wherever possible.
A pointer may be constant or variable. When it is variable, we can assign any appropriate memory address to the pointer at any time. When it is constant, we must assign an address at the point of instantiation and cannot change that address while the pointer is in scope; it must always point at the same memory. Note that the constness of the pointer does not affect the constness of the object being pointed at. However, if the object being pointed at is of a constant type, then the pointer must be a pointer to that constant type, regardless of whether the pointer itself is variable or constant. Thus we can point at a memory address in four different ways: a variable pointer to a variable type; a variable pointer to a constant type; a constant pointer to a variable type and; a constant pointer to a constant type. We define both aspects when we instantiate the pointer.
In C language a pointer is a variable that stores the address of another variable. In other words, pointer is a variable which points or references to another one variable or a memory location. The syntax for declaring a pointer variable is as follows.
type * pointer_variable_name
The character * tells the compiler that the variable declared is a pointer. The type specified the type of data stored in the location pointed by the variable.
The data stored in the location pointed by the pointer can be retrieved by a dereference operation.
eg:
int *a; // a is pointer of type integer
int b=10; // b is an integer with value 10
a = &b; // assigning a pointee - & will take the address of b
printf("%p %p %d %d",a,&b,*a,b); // Output will be in the following order
(1) the address of b
(2) address of b
(3) value of b - through pointer dereferencing
(4) value of b
Important: A pointer must be initialized or assigned before using it. By default all the pointers are bad pointers, which means the pointer is uninitialized. A dereference operation in a bad pointer may cause catastrophic impact on the system, since you might be accessing some unknown memory location.
In addition (as this question is also under the Java Programming section), it should be noted that Java DOES NOT support pointers. That is, the pointer concept does not exist in Java.
Pointer is a variable which store address of an other variable. To declare a pointer variable <syntax>:
<data-type> *<variable-name>;
example:
int *x;
now x will store an address. to find the address of any variable we use '&' in c.
example:
int y;
then address of y is &y;
but now x will store only address :
x= &y;
here x= address of variable and *x = value at the variable.
By assigning an address to them. All pointers must be initialised with the nullptr value (zero) if you cannot initialise with a valid address. If the address falls from scope, you must nullify the pointer.
Ideally you should avoid using raw pointers and use resource handles instead (smart pointers). However, C is not an object oriented programming language so raw pointers are unavoidable. The onus is therefore upon the programmer to ensure all pointers are initialised and nullified whenever they are no longer required.
Pointer variables are initialised by assigning an address to them. If you have no valid address, assign null (the zero address).
With a *
For example:
int *pointerToInt;
these are the memory locations i.e. addresses. try printing the content of pointer u'll get the memory location where they are stored. :-)
Use the pointer operator (*) immediately after the type.
normal variable contains the value of variable (ex.int or float) whereas pointer variable contains the address of another variable
Answergenerally we use simple pointer, void pointer,null pointer, structure pointer. Answerzero or more (unlimited).
Yes, two different pointer variables may point to the same memory location. Two issues, though... If your code thinks the two pointers represent two different allocations of memory, then you risk deallocating one and not realizing that the other is now invalid. You need to make sure that your compiler optimization settings for volatility are correct, otherwise the compiler could generate code that does not always work correctly.
... are usable. void pointer (generic pointer) : a special type of pointer which point to some data of no specific types. void *p; null pointer : a special type of pointer which point nowhere. it is usually used to check if a pointer is pointing to a null or free the pointer during deallocation of memory in dynamic memory allocation; it is define by using the predefine constant NULL int *p=NULL; wild pointer : uninitialized pointer. it hold a garbage value. i.e it is not pointing to any memory location yet. dangling pointer: pointer pointing to a destroyed variable. it usually happen during dynamic memory allocation when the object is destroyed but not free and the pointer is still pointing to the destroy object.
At any given point of time you cann't get the address of a variables of java program. This is meant for security purpose only.
They both mean the same thing; an array is a type of data structure (a linear structure). A pointer variable is just a variable like any other, but one that is used to specifically store a memory address. That memory address may contain a primitive data type, an array or other data structure, an object or a function. The type of the pointer determines how the data being pointed at is to be treated. Pointers must always be initialised before they are accessed, and those that are not specifically pointing at any reference should always be zeroed or nullified with the NULL value. This ensures that any non-NULL pointer is pointing at something valid. Remember that pointer variables are no different to any other variable insofar as they occupy memory of their own, and can therefore point to other pointer variables.
Pointer-variables are variables, so there is no difference.
In JAVA, all variables are reference variables, and there are no pointer variables. Even though the platform may implement them as pointers, they are not available as such. In C, no variables are reference variables. They are a C++ enhancement. In C++ a reference variable is syntactically the same as a pointer variable, except that the use of the indirection operator (*) is implicit. You do declare reference variables slightly differently than pointer variables but, once you do so, they can be treated as non-pointer variables. Reference variables also cannot be redefined once they have been initialized to point to some object. They are const. Structurally, there is no difference between a pointer variable and a reference variable. They are both still pointers. The compiler just makes it easier to treat reference variables and non-pointer variables the same way.
Answergenerally we use simple pointer, void pointer,null pointer, structure pointer. Answerzero or more (unlimited).
pointer r the variables created in RAM which store the address of a another variable
Yes. All string variables are pointers as are other arrays.
You can declare pointer-variables, if that's what you mean. Example: char *sample = "Sample";
Yes, two different pointer variables may point to the same memory location. Two issues, though... If your code thinks the two pointers represent two different allocations of memory, then you risk deallocating one and not realizing that the other is now invalid. You need to make sure that your compiler optimization settings for volatility are correct, otherwise the compiler could generate code that does not always work correctly.
A pointer is a variable used specifically to store a memory address. We say the variable "points to" the memory address because we can dereference the pointer to access the value stored at that address. The pointer's type determines how that dereferenced value will be interpreted. Being a variable, we can change the stored address and thus change which value we point at. This makes it possible for the same variable to refer to different objects in memory, which includes other pointer variables.
... are usable. void pointer (generic pointer) : a special type of pointer which point to some data of no specific types. void *p; null pointer : a special type of pointer which point nowhere. it is usually used to check if a pointer is pointing to a null or free the pointer during deallocation of memory in dynamic memory allocation; it is define by using the predefine constant NULL int *p=NULL; wild pointer : uninitialized pointer. it hold a garbage value. i.e it is not pointing to any memory location yet. dangling pointer: pointer pointing to a destroyed variable. it usually happen during dynamic memory allocation when the object is destroyed but not free and the pointer is still pointing to the destroy object.
Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.
At any given point of time you cann't get the address of a variables of java program. This is meant for security purpose only.
They both mean the same thing; an array is a type of data structure (a linear structure). A pointer variable is just a variable like any other, but one that is used to specifically store a memory address. That memory address may contain a primitive data type, an array or other data structure, an object or a function. The type of the pointer determines how the data being pointed at is to be treated. Pointers must always be initialised before they are accessed, and those that are not specifically pointing at any reference should always be zeroed or nullified with the NULL value. This ensures that any non-NULL pointer is pointing at something valid. Remember that pointer variables are no different to any other variable insofar as they occupy memory of their own, and can therefore point to other pointer variables.