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

C Pointers: Usage and Examples

The document provides an introduction to pointers in C programming, explaining their syntax and usage for storing variable addresses and performing operations like addition and checking for odd/even or prime numbers. It covers pointer arithmetic, including incrementing and decrementing pointers, and demonstrates how to use pointers with arrays and functions. Additionally, it includes example programs illustrating these concepts.

Uploaded by

balthapa2006
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)
3 views6 pages

C Pointers: Usage and Examples

The document provides an introduction to pointers in C programming, explaining their syntax and usage for storing variable addresses and performing operations like addition and checking for odd/even or prime numbers. It covers pointer arithmetic, including incrementing and decrementing pointers, and demonstrates how to use pointers with arrays and functions. Additionally, it includes example programs illustrating these concepts.

Uploaded by

balthapa2006
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

UNIT – 4

POINTERS
Introduction: -
Pointers are used to store the address of variables. Pointers are used to create
run-time memory.
Syntax: - datatype *ptr;
Example: -
#include<stdio.h>

void main()

int a=5, b=10;

printf("Address of a = %x",&a); //Hexadecimal value

printf("\nAddress of b = %x",&b); //Hexadecimal value

printf("\nValue of a =%d and b = %d",a,b);

Ques – WAP to add two numbers using pointers?


Sol -
#include<stdio.h>
void main()
{

int a, b,c, *p, *q;

printf("Enter two numbers: ");

scanf("%d%d",&a,&b);

p=&a;

q=&b;

c=*p+*q;

printf("\nSum is = %d",c);

}
Ques – WAP to check a number is odd or even using pointers?
Sol –
#include<stdio.h>

void main()

int a, *p;

printf("Enter number: ");

scanf("%d",&a);

p=&a;

if(*p%2==1)

printf("Number is odd");

else

printf("Number is Even");

Ques – WAP to check a number is prime or not using pointers?


Sol –
#include<stdio.h>

void main()

int a, *p, count=0, i;

printf("Enter number: ");

scanf("%d",&a);

p=&a;

for(i=1;i<=*p;i++)
{

if(*p%i==0)

count++;

if(count==2)

printf("Number is prime");

else

printf("Number is not prime");

Pointer Arithmetic: -
The pointer Arithmetic refer to the legal or valid arithmetic operations
that can be performed on a pointer. C pointer arithmetic operation are different
from the general arithmetic operations.
1. Increment and Decrement of a pointer.
2. Addition & subtraction of integer to a pointer
3. Subtraction two pointers of the same type.
4. Comparison of Pointers.

Example: -
#include<stdio.h>
int main(){
int number=50;
int *p;//pointer to int
p=&number;//stores the address of number variable
printf("Address of p variable is %u \n",p);
p=p+1;
printf("After increment: Address of p variable is %u \n",p); // in our case, p
will get incremented by 4 bytes.
p=p-2;
printf("After Decrement: Address of p variable is %u \n",p);
return 0;
}
Pointer with Array: -
Addressing of array
0 1 2 3 4 5 6 7 8 9
Int: -
100 104 108 112 116 120 124 128 132 136
0 1 2 3 4 5 6 7 8 9
Char: -
100 1010 102 103 104 105 106 107 108 109
0 1 2 3 4 5 6 7 8 9
Float: -
200 204 208 212 216 220 224 228 232 236

Ques: - Write a program Accept and Display array using pointer?

Sol: -
#include<stdio.h>

void main()

int a[5],i;

int *p;

for(i=0;i<5;i++)

printf("\nEnter Elements: ");

scanf("%d",&a[i]);

p = &a[0];

for(i=0;i<5;i++)

printf("\nArray value is: %d\tAddress is: %x",*(p+i), &a[i]);

}
Pointer With function: -
A function pointer is a variable that stores the address of a function.
#include<stdio.h>
void add(int*,int*);
void main()

int a,b,*p,*q;

printf("Enter two numbers: ");

scanf("%d%d",&a,&b);

p=&a;

q=&b;

add(p,q);

void add(int *p,int *q)

int c;

c=*p+*q;

printf("\nAddition = %d",c);

}
Ques: - Write a C program to return multiple values using function string?
Sol: -
#include <stdio.h>
#include <string.h>
void getNameAndAge(char *, int *);

int main()

char name[50], *p;

int age, *q;

p=&name;

q=&age;

getNameAndAge(p, q);

return 0;

void getNameAndAge(char *p, int *q)

strcpy(p, "Jane Smith");

q = 25;

printf("Name: %s, Age: %d\n", p, q);

You might also like