Address in C: Reference Operator (&) and Dereference Operator ( )
Address in C: Reference Operator (&) and Dereference Operator ( )
Before you get into the concept of pointers, let's first get familiar with address in C.
If you have a variable var in your program, &var will give you its address in the
memory, where & is commonly called the reference operator.
You must have seen this notation while using scanf() function. It was used in the
function to store the user inputted value in the address of var.
scanf("%d", &var);
Pointer variables
In C, you can create a special variable that stores the address (rather than the
value). This variable is called pointer variable or simply a pointer.
data_type* pointer_variable_name;
int* p; or int *p
Likewise, there is another operator that gets you the value from the address, it is
called a dereference operator *.
Int* pc;
Int c = 22;
pc = &c;
*pc = 2;
int c, *pc;
C - Pointer arithmetic
There are four arithmetic operators that can be used on pointers: ++, --,
+, and -
After the above operation, the ptr will point to the location 1004 because
each time ptr is incremented, it will point to the next integer location
which is 4 bytes next to the current location
Pointer Comparisons
Pointers may be compared by using relational operators, such as ==, <,
and >. If p1 and p2 point to variables that are related to each other,
such as elements of the same array, then p1 and p2 can be meaningfully
compared.
C Pointers and Arrays
Now, check this simple program. This program prints address of each individual
element of an array.
#include <stdio.h>
int main()
{
int x[4];
int i;
return 0;
}
When you run the program, the output will be something like:
&x[0] = 1450734448
&x[1] = 1450734452
&x[2] = 1450734456
&x[3] = 1450734460
Address of array x: 1450734448
int x[4];
145073
145073
From the above example, it's clear that x and &x[0] both contains the same address.
Hence, &x[0] is equivalent to x.
Similarly,
Array names are converted to pointers, that is they point to begin element, x and &x[0] both
contains the same address
There are few cases where array name doesn't decay into a pointer. To learn more,
visit: When does array name doesn't decay into a pointer?
ptr = &x[2];
return 0;
}
*ptr = 3
*ptr+1 = 4
*ptr-1 = 2
C Call by Reference: Using pointers
#include <stdio.h>
void swap(int *n1, int *n2);
int main()
{
int num1 = 5, num2 = 10;
int * myFunction() {
#include <stdio.h>
#include <time.h>
int * getRandom( ) {
int i;
r[i] = rand();
printf("%d\n", r[i] );
return r;
int main () {
/* a pointer to an int */
int *p;
int i;
p = getRandom();
return 0;
C - Pointer to Pointer
When we define a pointer to a pointer, the first pointer contains the address
of the second pointer, which points to the location that contains the actual
value as shown below.
A variable that is a pointer to a pointer must be declared as such. This is
done by placing an additional asterisk in front of its name. For example,
the following declaration declares a pointer to a pointer of type int −
int **var;
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var;
pptr = &ptr;
return 0;
Sometimes, the size of array you declared may be insufficient. To solve this issue,
you can allocate memory manually during run-time. This is known as dynamic
memory allocation in C programming.
C malloc()
The name "malloc" stands for memory allocation.
Syntax of malloc()
ptr = (cast-type*) malloc(byte-size)
Example:
Syntax of calloc()
ptr = (cast-type*)calloc(n, element-size);
Example:
This statement allocates contiguous space in memory for 25 elements each with the
size of float
C free()
Dynamically allocated memory created with either calloc() or malloc() doesn't get
freed on their own. You must explicitly use free() to release the space.
Syntax of free()
free(ptr);
This statement frees the space allocated in the memory pointed by ptr.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
C realloc()
If the dynamically allocated memory is insufficient or more than required, you can
change the size of previously allocated memory using realloc() function
Syntax of realloc()
ptr = realloc(ptr, x);
Here, ptr is reallocated with new size x.
QUESTIONS
What is indirection?
If you declare a variable, its name is a direct reference to its value. If you have a pointer to a
variable, or any other object in memory, you have an indirect reference to its value. If p is a
pointer, the value of p is the address of the object. *p means "apply the indirection operator to p";
its value is the value of the object that ppoints to. (Some people would read it as "Go indirect
on p.")
What is a null pointer?
There are times when it's necessary to have a pointer that doesn't point to anything. The
macro NULL, defined in <stddef.h>, has a value that's guaranteed to be different from any valid
pointer. NULL is a literal zero
What is a void pointer?
A void pointer is a C convention for "a raw address." The compiler has no idea what type of
object a voidpointer "really points to." If you write
int *ip;
ip points to an int. If you write
void *p;
p doesn't point to a void!
In C and C++, any time you need a void pointer, you can use another pointer type. For example,
if you have a char*, you can pass it to a function that expects a void*. You don't even need to
cast it. In C (but not in C++), you can use a void* any time you need any kind of pointer, without
casting. (In C++, you need to cast it.)
When is a void pointer used?
A void pointer is used for working with raw memory or for passing a pointer to an unspecified
type.
Can you subtract pointers from each other? Why would you?
If you have two pointers into the same array, you can subtract them. The answer is the number
of elements between the two elements.
if ( /* ... */ )
{
p = NULL;
}
else
{
p = /* something else */;
}
/* ... */
if ( p == 0 )
then yes, NULL is always equal to 0. That's the whole point of the definition of a null pointer.
If you mean "is stored the same way as an integer zero," the answer is no, not necessarily.
That's the most common way to store a null pointer. On some machines, a different
representation is used
What does it mean when a pointer is used in an if statement?
Any time a pointer is used as a condition, it means "Is this a non-null pointer?" A pointer can be
used in an if, while, for, or do/while statement, or in a conditional expression
Can you add pointers together? Why would you?
No, you can't add pointers together.
To set up pf to point to the strcmp() function, you want a declaration that looks just like
the strcmp()function's declaration, but that has *pf rather than strcmp:
Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and
it's allocation is dealt with when the program is compiled.
There used to be a C function that any programmer could use for allocating memory off the
stack. The memory was automatically deallocated when the calling function returned. This was a
dangerous function to call; it's not available anymore.
20. What is the heap?
Heap is used for dynamic memory allocation.The heap is where malloc(), calloc(),
and realloc() get memory.
Getting memory from the heap is much slower than getting it from the stack. On the other hand,
the heap is much more flexible than the stack. Memory can be allocated at any time and
deallocated in any order. Such memory isn't deallocated automatically; you have to call free().