[go: up one dir, main page]

0% found this document useful (0 votes)
411 views8 pages

Pointers in C

- Pointers in C store the address of another variable rather than the variable's value. - Pointer variables can point to data of any type, such as int, float, char, etc. - The & operator returns the address of a variable, while the * operator accesses the value at a pointer's address. - Pointers allow dynamic memory allocation at runtime and access to the values of other variables through their addresses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
411 views8 pages

Pointers in C

- Pointers in C store the address of another variable rather than the variable's value. - Pointer variables can point to data of any type, such as int, float, char, etc. - The & operator returns the address of a variable, while the * operator accesses the value at a pointer's address. - Pointers allow dynamic memory allocation at runtime and access to the values of other variables through their addresses.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

C – Pointer

 Pointers in C language is a variable that stores/points the address of another variable. A


Pointer in C is used to allocate memory dynamically i.e. at run time.
 The pointer variable might be belonging to any of the data type such as int, float, char,
double, short etc.
 Pointer Syntax : data_type *var_name; Example : int *p; char *p;
 Reference operator (&) and Dereference operator (*)
 Reference operator (&)  It gives you the address of a variable.
 Dereference operator (*).  that gets you the value from the address

KEY POINTS TO REMEMBER ABOUT POINTERS IN C:

 Normal variable stores the value whereas pointer variable stores the address of the
variable.
 The content of the C pointer always be a whole number i.e. address.
 Always C pointer is initialized to null, i.e. int *p = null.
 The value of null pointer is 0.
 & symbol is used to get the address of the variable.
 * symbol is used to get the value of the variable that the pointer is pointing to.
 If a pointer in C is assigned to NULL, it means it is pointing to nothing.
 Two pointers can be subtracted to know how many elements are available between these
two pointers.
 But, Pointer addition, multiplication, division are not allowed.
 The size of any pointer is 2 byte (for 16 bit compiler).
EXAMPLE PROGRAM FOR POINTERS IN C:

#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}

Example To Demonstrate Working of Pointers

/* Source code to demonstrate, handling of pointers in C program */


#include <stdio.h>
int main(){
int* pc;
int c;
c=22;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
pc=&c;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
c=11;
printf("Address of pointer pc:%u\n",pc);
printf("Content of pointer pc:%d\n\n",*pc);
*pc=2;
printf("Address of c:%u\n",&c);
printf("Value of c:%d\n\n",c);
return 0;
}

Output

Address of c: 2686784
Value of c: 22
Address of pointer pc: 2686784
Content of pointer pc: 22
Address of pointer pc: 2686784
Content of pointer pc: 11
Address of c: 2686784
Value of c: 2

C - Array of pointers
#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i;

for (i = 0; i < MAX; i++) {


printf("Value of var[%d] = %d\n", i, var[i] );
}

return 0;
}
When the above code is compiled and executed, it produces the following
result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
#include <stdio.h>

const int MAX = 3;

int main () {

int var[] = {10, 100, 200};


int i, *ptr[MAX];

for ( i = 0; i < MAX; i++) {


ptr[i] = &var[i]; /* assign the address of integer. */
}

for ( i = 0; i < MAX; i++) {


printf("Value of var[%d] = %d\n", i, *ptr[i] );
}

return 0;
}

When the above code is compiled and executed, it produces the following
result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
Question 1
What will be the output?
#include<stdio.h>
int main()
{
int a[] = { 1, 2, 3, 4, 5} ;
int *ptr;
ptr = a;
printf(" %d ", *( ptr + 1) );

return 0;
}
output 2
Description:
It is possible to assign an array to a pointer. so, when ptr = a; is executed the address of element
a[0] is assigned to ptr and *ptr gives the value of element a[0]. When *(ptr + n) is executed the
value at the nth location in the array is accessed.
Question 2
What will be the output?
#include<stdio.h>
int main()
{
int a = 5;
int *ptr ;
ptr = &a;
*ptr = *ptr * 3;
printf("%d", a);

return 0;
}
Output: 15
Description:
ptr = &a; copies the address of a in ptr making *ptr = a and the statement *ptr = *ptr * 3; can be
written as a = a * 3; making a as 15.
Question 2
What will be the output?
#include<stdio.h>
int main()
{
int i = 6, *j, k;
j = &i;
printf("%d\n", i * *j * i + *j);
return 0;
}
Output:222
Description:
According to BODMAS rule multiplication is given higher priority. In the expression i * *j * i +
*j;, i * *j *i will be evaluated first and gives result 216 and then adding *j i.e., i = 6 the output
becomes 222.
Question 4
What will be the output?
#include<stdio.h>
int main()
{
int x = 20, *y, *z;
// Assume address of x is 500 and
// integer is 4 byte size
y = &x;
z = y;
*y++;
*z++;
x++;
printf("x = %d, y = %d, z = %d \n", x, y, z);
return 0;
}
Output:x=21 y=504 z=504
Description:
In the beginning, the address of x is assigned to y and then y to z, it makes y and z similar. when
the pointer variables are incremented there value is added with the size of the variable, in this
case, y and z are incremented by 4.

Question 5
What will be the output?
#include<stdio.h>
int main()
{
int x = 10;
int *y, **z;
y = &x;
z = &y;
printf("x = %d, y = %d, z = %d\n", x, *y, **z);
return 0;
}
Output:x=10 y=10 z=10
Description:
*y is a pointer variable whereas **z is a pointer to a pointer variable. *y gives the value at the
address it holds and **z searches twice i.e., it first takes the value at the address it holds and then
gives the value at that address.
22.What will be the output of following program ?
#include <stdio.h>
int main()
{
char *str []={"AAAAA","BBBBB","CCCCC","DDDDD"};
char **sptr []={str+3,str+2,str+1,str};
char ***pp;
pp=sptr;
++pp;
printf("%s",**++pp+2);
return 0;
}
BBBBB
CCCCC
BBB
Error
Answer
Correct Answer - 3
BBB
*str is a array pointer of string, **sptr is array pointer(double pointer) that is pointing to str
strings in reverse order. ***pp also a pointer that is pointing sptr base address.
++pp will point to 1st index of sptr that contain str+2 ("CCCCC").
in printf("%s",**++pp+2); ++pp will point to str+1, and **++pp, value stored @ str+1
("BBBBB).
and (**++pp)+2 will point the 2nd index of "BBBBB", hence BBB will print.

You might also like