0% found this document useful (0 votes)
135 views6 pages

Practical No 6

This document discusses pointers in C programming. It defines a pointer as a variable that stores the address of another variable. It explains the "&" operator, which returns the address of a variable, and the "*" operator, which accesses the value at a given address. Examples are provided to demonstrate declaring pointers, assigning addresses to pointers, and accessing variable values through pointers. The document also includes code samples showing pointer usage and a program that prints all permutations of a string using pointers.

Uploaded by

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

Practical No 6

This document discusses pointers in C programming. It defines a pointer as a variable that stores the address of another variable. It explains the "&" operator, which returns the address of a variable, and the "*" operator, which accesses the value at a given address. Examples are provided to demonstrate declaring pointers, assigning addresses to pointers, and accessing variable values through pointers. The document also includes code samples showing pointer usage and a program that prints all permutations of a string using pointers.

Uploaded by

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

Practical no.

7
To study and perform program based on the use of Pointers.

Theory:
A pointer is a variable that stores the address of another variable. Unlike other
variables that hold values of a certain type, pointer holds the address of a
variable. For example, an integer variable holds (or you can say stores) an
integer value, however an integer pointer holds the address of a integer variable.
In this practical, we will discuss pointers in C programming with the help of
examples.

C Pointers – Operators that are used with Pointers

Lets discuss the operators & and * that are used with Pointers in C.

“Address of”(&) Operator

We have already seen in the first example that we can display the address of a variable using
ampersand sign. I have used &num to access the address of variable num. The & operator is
also known as “Address of” Operator.

printf("Address of var is: %p", &num);

Point to note: %p is a format specifier which is used for displaying the address in hex format.
Now that you know how to get the address of a variable but how to store that address in some
other variable? That’s where pointers comes into picture. As mentioned in the beginning of this
guide, pointers in C programming are used for holding the address of another variables.

Pointer is just like another variable, the main difference is that it stores address of another
variable rather than a value.

“Value at Address”(*) Operator

The * Operator is also known as Value at address operator.

How to declare a pointer?

int *p1 /*Pointer to an integer variable*/

double *p2 /*Pointer to a variable of data type double*/

char *p3 /*Pointer to a character variable*/

float *p4 /*pointer to a float variable*/


The above are the few examples of pointer declarations. If you need a pointer to store the
address of integer variable then the data type of the pointer should be int. Same case is with
the other data types.

By using * operator we can access the value of a variable through a pointer.


For example:

double a = 10;

double *p;

p = &a;

*p would give us the value of the variable a. The following statement would display 10 as
output.

printf("%d", *p);

Similarly if we assign a value to *pointer like this:

*p = 200;

It would change the value of variable a. The statement above will change the value of a from 10
to 200.

/*C program to demonstrate example of double pointer (pointer


to pointer).*/
#include <stdio.h>

int main()
{
int a; //integer variable
int *p1; //pointer to an integer
int **p2; //pointer to an integer pointer

p1=&a; //assign address of a


p2=&p1; //assign address of p1

a=100; //assign 100 to a

//access the value of a using p1 and p2


printf("\nValue of a (using p1): %d",*p1);
printf("\nValue of a (using p2): %d",**p2);
//change the value of a using p1
*p1=200;
printf("\nValue of a: %d",*p1);
//change the value of a using p2
**p2=200;
printf("\nValue of a: %d",**p2);

return 0;
}

Write a program in C to print all permutations of a given string using pointers.

C Code:
#include <stdio.h>

#include <string.h>

void changePosition(char *ch1, char *ch2)

char tmp;

tmp = *ch1;

*ch1 = *ch2;

*ch2 = tmp;

void charPermu(char *cht, int stno, int endno)

int i;

if (stno == endno)

printf("%s ", cht);

else

{
for (i = stno; i <= endno; i++)

changePosition((cht+stno), (cht+i));

charPermu(cht, stno+1, endno);

changePosition((cht+stno), (cht+i));

int main()

char str[] = "abcd";

printf("\n\n Pointer : Generate permutations of a given


string :\n");

printf("---------------------------------------------------
-----\n");

int n = strlen(str);

printf(" The permutations of the string are : \n");

charPermu(str, 0, n-1);

printf("\n\n");

return 0;

OUTPUT:

EXAMPLE PROGRAM FOR C STRUCTURE USING POINTER:


In this program, “record1” is normal structure variable
and “ptr” is pointer structure variable. As you know, Dot(.)
operator is used to access the data using normal structure
variable and arrow(->) is used to access data using pointer
variable.
#include <stdio.h>

#include <string.h>

struct student

int id;

char name[30];

float percentage;

};

int main()

int i;

struct student record1 = {1, "Raju", 90.5};

struct student *ptr;

ptr = &record1;

printf("Records of STUDENT1: \n");

printf(" Id is: %d \n", ptr->id);

printf(" Name is: %s \n", ptr->name);

printf(" Percentage is: %f \n\n", ptr->percentage);

return 0;

}
OUTPUT:

Conclusion:

In this way we had studied and perform program based on


pointers.

You might also like