0% found this document useful (0 votes)
1 views

address of operator in C

The document explains the concepts of address of operator (&) and indirection operator (*) in C programming, detailing their usage and examples. It also covers pointers, including how to declare, assign, and manipulate them, as well as their application in accessing array elements and function pointers. Additionally, it provides code examples to illustrate these concepts in practice.

Uploaded by

JOHN BALA KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

address of operator in C

The document explains the concepts of address of operator (&) and indirection operator (*) in C programming, detailing their usage and examples. It also covers pointers, including how to declare, assign, and manipulate them, as well as their application in accessing array elements and function pointers. Additionally, it provides code examples to illustrate these concepts in practice.

Uploaded by

JOHN BALA KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Address of operator

• & is known as address of operator


• It is an unary operator.
• Operand must be the name of variable.
• & operator gives address of variable.
• & is also known as referencing operator.
• If you have a variable x in your program, &x will give
you its address in the memory.
• We have used address numerous times while using
the scanf() function.
• scanf("%d", &x); Here, the value entered by the user is
stored in the address of x variable.
Address of operator
• Let's take a working example.
#include <stdio.h>
int main()
{
int x = 5;
x Name of
printf(“x: %d\n", x); memory block
// Notice the use of & before x
5 Content of
printf("address of x: %u", &x); memory block
return 0; Memory
block 2686778 Address of
} memory block
• Output
x: 5
address of x: 2686778

Note: You will probably get a different address when you run the
above code.
Indirection Operator or dereferencing operator

• * is indirection pointer.
• It is also known as dereferencing pointer.
• It is an Unary operator
• It takes address as an argument
• * returns the Content/Container whose
address is its argument
Indirection Operator
• Let's take a working example.
#include <stdio.h>
int main()
{
int x = 5;
printf(“%d\n",x);
printf("%u",&x);
printf("%u",*&x);
return 0;
}
• Output
5
2686778
5
• The pointer in C language is a variable which
stores the address of another variable.
• This variable can be of type int, char, array,
function, or any other pointer.
• The size of the pointer depends on the
architecture. However, in 32-bit architecture
the size of a pointer is 2 byte.
Pointer Syntax
Here is how we can declare pointers.
int* p;
• Here, we have declared a pointer p of int type.

You can also declare pointers in these ways.


int *p1;
int * p2;

Let's take another example of declaring pointers.


int* p1, p2;
• Here, we have declared a pointer p1 and a normal
variable p2.
Assigning addresses to Pointers

• Let's take an example.


int* pc, c;
c = 5;
pc = &c;
• Here, 5 is assigned to the c variable. And, the
address of c is assigned to the pc pointer.
Get Value of Thing Pointed by
Pointers
• To get the value of the thing pointed by the pointers,
we use the * operator. For example:
int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc); // Output: 5
• Here, the address of c is assigned to the pc pointer. To
get the value stored in that address, we used *pc.

• Note: In the above example, pc is a pointer, not *pc.


You cannot and should not do something like *pc =
&c;
• By the way, * is called the dereference operator (when
working with pointers). It operates on a pointer and
gives the value stored in that pointer.
Changing Value Pointed by Pointers
• Let's take an example.
int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
• We have assigned the address of c to the pc pointer.
• Then, we changed the value of c to 1. Since pc and the address of c is the
same, *pc gives us 1.

• Let's take another example.


int* pc, c;
c = 5;
pc = &c;
*pc = 1;//
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1
• We have assigned the address of c to the pc pointer.
• Then, we changed *pc to 1 using *pc = 1;. Since pc and the address of c is
the same, c will be equal to 1.
• Let's take one more example.
int* pc, c, d;
c = 5;
d = -15;
pc = &c;
printf("%d", *pc); // Output: 5
pc = &d;
printf("%d", *pc); // Ouptut: -15

• Initially, the address of c is assigned to the pc pointer


using pc = &c;. Since c is 5, *pc gives us 5.
• Then, the address of d is assigned to the pc pointer
using pc = &d;. Since d is -15, *pc gives us -15.
int main()
{
Output:
int x = 5,*j; 5 2048
j=&x; 5 2048
2048
printf(“%d %u\n",x,j);
printf(“%d %u",*j,&x);
printf("%u",*&j);
return 0;
}
Example
#include <stdio.h>
int main() Output
{
Address of c: 2686784
int* pc, c; Value of c: 22
c = 22; Address of pointer pc: 2686784
printf("Address of c: %p\n", &c); Content of pointer pc: 22
printf("Value of c: %d\n\n", c); // 22 Address of pointer pc: 2686784
Content of pointer pc: 11
pc = &c;
Address of c: 2686784
printf("Address of pointer pc: %p\n", pc); Value of c: 2
printf("Content of pointer pc: %d\n\n", *pc); // 22
c = 11;
printf("Address of pointer pc: %p\n", pc);
printf("Content of pointer pc: %d\n\n", *pc); // 11
*pc = 2;
printf("Address of c: %p\n", &c);
printf("Value of c: %d\n\n", c); // 2
return 0;
}
C Pointers & Arrays with Examples

• Traditionally, we access the array elements


using its index, but this method can be
eliminated by using pointers. Pointers make it
easy to access each array element.
#include <stdio.h>
int main() Output:
{ 1
int a[5]={1,2,3,4,5}; //array initialization
2
int *p; //pointer declaration
/*the ptr points to the first element of the array*/ 3
p=a; /*We can also type simply ptr==&a[0] */ 4
printf("Printing the array elements using pointer\n"); 5
for(int i=0;i<5;i++) //loop for traversing array elements
{
printf("\n%x",*p); //printing array elements
p++; //incrementing to the next element, you can also
write p=p+1 }
}
return 0;
}
#include<stdio.h>
void main ()
{
int arr[5] = {1, 2, 3, 4, 5}; OUTPUT:
int *p = arr; printing array elements... 1 2 3 4 5
int i;
printf("printing array elements...\n");
for(i = 0; i< 5; i++)
{
printf("%d ",*(p+i));
}
}

• Pointer +n = pointer + size of (type of


pointer)*n
• Adding a particular number to a pointer will
move the pointer location to the value
obtained by an addition operation. Suppose p
is a pointer that currently points to the
memory location 0 if we perform following
addition operation, p+1 then it will execute in
this manner:
C Function Pointer
Declaration of a function pointer

• Syntax of function pointer


return type (*ptr_name)(type1, type2…);

• For example:

int (*ip) (int);


In the above declaration, *ip is a pointer that points to a function
which returns an int value and accepts an integer value as an
argument.

float (*fp) (float);


In the above declaration, *fp is a pointer that points to a function
that returns a float value and accepts a float value as an
argument.
• float (*fp) (int , int); // Declaration of a function pointer.

• float func( int , int ); // Declaration of function.

• fp = func; // Assigning address of func to the fp pointer.


• In the above declaration, 'fp' pointer contains the address of
the 'func' function.

• Note: Declaration of a function is necessary before assigning the


address of a function to the function pointer.
Calling a function through a function

pointer
We already know how to call a function in the usual way. Now, we will see how to
call a function using a function pointer.

• Suppose we declare a function as given below:


float func(int , int); // Declaration of a function.

• Calling an above function using a usual way is given below:


result = func(a , b); // Calling a function using usual ways.

• Calling a function using a function pointer is given below:


result = (*fp)( a , b); // Calling a function using function pointer.
Or
result = fp(a , b); // Calling a function using function pointer, and indirection ope
rator can be removed.

• The effect of calling a function by its name or function pointer is the same. If we are
using the function pointer, we can omit the indirection operator as we did in the
second case. Still, we use the indirection operator as it makes it clear to the user that
we are using a function pointer.
Example
#include <stdio.h>
int add(int,int);
int main()
{ Output:
int a,b;
int (*ip)(int,int);
int result;
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b);
printf("Value after addition is : %d",result);

return 0;
}
int add(int a,int b)
{
int c=a+b;
return c;
}

You might also like