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

Pointer

C language

Uploaded by

lunadevil07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Pointer

C language

Uploaded by

lunadevil07
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

EX NO:6 POINTERS

28/11/24

AIM:
To solve the problems given using C programing language topic pointers.

Question 1
Print size of different data types using pointers.
Source code:
#include <stdio.h>
void main() {
char c;
printf("Enter the type of data type (f for float, i for int, l for long, d for double): ");
scanf(" %c", &c);
switch (c) {
case 'f': {
float n;
float *pa = &n;
printf("Enter a value: ");
scanf("%f", &n);
printf("Value: %f, Size: %zu bytes\n", *pa, sizeof(*pa));
break;
} case 'i': {
int n;
int *pa = &n;
printf("Enter a value: ");
scanf("%d", &n);
printf("Value: %d, Size: %zu bytes\n", *pa, sizeof(*pa));

24I310
break;
} case 'l': {
long n;
long *pa = &n;
printf("Enter a value: ");
scanf("%ld", &n);
printf("Value: %ld, Size: %u bytes", *pa, sizeof(*pa));
break;
} case 'd': {
double n;
double *pa = &n;
printf("Enter a value: ");
scanf("%lf", &n);
printf("Value: %lf, Size: %zu bytes\n", *pa, sizeof(*pa));
break;
} default:
printf("Enter a valid symbol (f, i, l, d).\n");
break; ;}}

output:

Question 2
Change the value of constant integer using pointers.
Source code:
#include<stdio.h>
void main()

24I310
{int n;
printf("enter the value of n:");
int *pa=&n;
scanf("%d",&n);
printf("%d is the value of n.\n",n);
printf("enter the value you want to change with this value %d:",n);
scanf("%d",&*pa);
printf("%d is the new value of n\n",*pa);
printf("%d is the value of n.\n",n);
}
output:

Question 3
Find the maximum number between three numbers using a pointer.
Source code:
#include<stdio.h>
void main()
{int n,m,b;
printf("enter the value of n,m,b:");
int *pa=&n;
int *pb=&m;

24I310
int *pc=&b;
scanf("%d %d %d",&n,&m,&b);
if(*pa>*pb&&*pa>*pc)
{ printf("%d is the largest number",n);}
else if (*pb>*pa&&*pb>*pc)
{ printf("%d is the largest number",m);}
else
{ printf("%d is the largest number",b);}}
output:

Question 4
Declares and initializes (to any value you like) a double, an int, and a char. Next
declare and
initialize a pointers cp1,cp2,cp3 to each of the three variables. Your program
should print the
address of, and value stored in, and the memory size (in bytes) of each of the six
variables.
Perform arithmetic addition by adding value to 2 to all each pointers declared.
Source code:
#include <stdio.h>

void main() {

24I310
int n;
int *pa = &n;
printf("Enter an integer value: ");
scanf("%d", &n);
printf("Integer value: %d, Size: %zu bytes, Address: %p\n", *pa, sizeof(*pa),
(void*)pa);
*pa = *pa + 2;
printf("New integer value: %d\n", *pa);

double m;
double *pb = &m;
printf("Enter a double value: ");
scanf("%lf", &m);
printf("Double value: %lf, Size: %zu bytes, Address: %p\n", *pb, sizeof(*pb),
(void*)pb);
*pb = *pb + 2;
printf("New double value: %lf\n", *pb);

char d;
char *pc = &d;
printf("Enter a char value: ");
scanf(" %c", &d);
printf("Char value: %c, Size: %zu bytes, Address: %p\n", *pc, sizeof(*pc),
(void*)pc);

24I310
*pc = *pc + 2;
printf("New char value: %c\n", *pc);
}
output:

Question 5
Complete the function void update(int *a,int *b). It receives two integer pointers,
int* a and
int* b. Set the value of a to their sum, b and to their absolute difference. There is
no return
value, and no return statement is needed.
a’ = a+b
b’ = |a-b|
Source code:
#include <stdio.h>
#include <stdlib.h>
void main() {
int a,b;
int *pa=&a;
int *pb=&b;

24I310
printf("enter the value of a and b: ");
scanf("%d %d",&(*pa),&(*pb));
a=(*pa)+(*pb);
b=abs((*pa)-(*pb));
printf("%d is the sum of two numbers\n",a);
printf("%d is the difference of two numbers",b);}
output:

Question 6
Any program to demonstrate how a function returns a pointer.
Source code:
#include <stdio.h>
int* getPointerToInteger() {
static int num = 42;
return &num;}
void main() {
int *ptr = getPointerToInteger();
printf("The value of the integer is: %d\n", *ptr);
printf("The address of the integer is: %p\n", (void*)ptr);
}
output:

24I310
Question 7
Perform the task using function taking a function pointer as an argument. Input
numbers from
the user. Perform functions of sin, cos and square.
Hint: Declare function compute_sum twice, passing it a pointer to the library
function sin the
first time, and a pointer to function cos the second time. Use standard library
function &#39;sin()&#39; as
the pointed-to function. Use standard library function &#39;cos()&#39; as the
pointed-to function. Use
user-defined function &#39;square()&#39; as the pointed-to function.
Source code:
#include <stdio.h>
#include <math.h>
double square(double x) {
return x * x;
}
double compute_sum(double (*func)(double), double x, double y) {
return func(x) + func(y);
}
void main() {
double num1, num2;
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);

24I310
double result_sin = compute_sum(sin, num1, num2);
printf("Sum of sin(%lf) and sin(%lf): %lf\n", num1, num2, result_sin);

double result_cos = compute_sum(cos, num1, num2);


printf("Sum of cos(%lf) and cos(%lf): %lf\n", num1, num2, result_cos);

double result_square = compute_sum(square, num1, num2);


printf("Sum of square(%lf) and square(%lf): %lf\n", num1, num2,
result_square);
}
output:

Question 8
A local discount store has a policy of putting labels with dates on all of its new
merchandise.
If an item has not sold within two weeks the store discounts the item by 25% for
the third
week,50% for the fourth week, and 75% for the fifth week. After that no
additional discounts
are given. Develop the function new-price and declare function pointer nptr
which takes the
natural price of an item and the number of weeks since the item was dated and
produces the
selling price of the item..
Source code:
#include <stdio.h>

24I310
double new_price(double price, int weeks) {
if (weeks >= 1 && weeks <= 2) {
return price;
} else if (weeks == 3) {
return price * 0.75;
} else if (weeks == 4) {
return price * 0.50;
} else if (weeks == 5) {
return price * 0.25;
} else {
return price; }}void main() {
double original_price;
int weeks;
double (*nptr)(double, int) = new_price;
printf("Enter the original price of the item: ");
scanf("%lf", &original_price);
printf("Enter the number of weeks since the item was dated: ");
scanf("%d", &weeks);
double final_price = nptr(original_price, weeks);
printf("The selling price of the item after %d week(s) is: %.2f\n", weeks,
final_price);
}
output:

24I310
24I310

You might also like