0% found this document useful (0 votes)
11 views7 pages

0032 - Lab Activity 1

The document outlines a lab activity for CSE203 focusing on advanced C programming with pointers. It includes various programming exercises demonstrating pointer declaration, initialization, arithmetic, dynamic memory allocation, and string manipulation. Each exercise is accompanied by sample code to illustrate the concepts effectively.
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)
11 views7 pages

0032 - Lab Activity 1

The document outlines a lab activity for CSE203 focusing on advanced C programming with pointers. It includes various programming exercises demonstrating pointer declaration, initialization, arithmetic, dynamic memory allocation, and string manipulation. Each exercise is accompanied by sample code to illustrate the concepts effectively.
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/ 7

CSE203 Object Oriented Programming

Lab Activity 1: Advanced C Programming

Topics covered: Pointers


Software to be used: Dev C++

Roll No.__________ ​ Name: ________________ ​ Section No.: _____

Write a C program to understand Pointer concepts:

1. Write a C program to show usage of Pointer Declaration and Initialization.


#include<stdio.h>

int main(){

int a = 5;

int* ptr = &a;

return 0;

write something here …

2. Printing variable addresses and values using C.


#include<stdio.h>

int main(){

int a ;

a=5;

int* p = &a;

printf("value of a : %d\n", *p);

printf("value of a : %d\n", a);

printf("memory address of a : %d\n", p);

printf("memory address of a : %d\n", &a);


printf("memory address of p : %d\n", &p);

return 0;

3. Pointer Arithmetic example.


#include<stdio.h>

int main(){

float arr[] = {10.0, 20.0, 30.0, 40.0};

float* ptr = arr;

for (int i = 0; i < 4; i++)

printf("Value at arr[%d] = %f\n", i, *ptr);

printf("Value at arr[%d] = %d\n", i, ptr);

ptr++;

return 0;

4. Dynamic Memory Allocation Using Pointers


#include<stdio.h>

#include<stdlib.h>
int main(){

int *ptr = (int *)malloc(sizeof(int));

if (ptr == NULL)

printf("memory allocation failed.\n");

return 1;

*ptr = 101;

printf("Value of dynamic allocated memory : %d\n", *ptr);

free(ptr);

return 0;

5. Swapping Two Numbers Using Pointers and Function.


#include<stdio.h>

void swap(int *x, int *y){

int temp = *x;

*x = *y;

*y = temp;

int main(){

int a, b;

scanf("%d", &a);

scanf("%d", &b);
printf("before swap : a = %d and b = %d\n", a, b);

swap(&a, &b);

printf("after swap : a = %d and b = %d\n", a, b);

return 0;

6. Passing Array to a Function Using Pointers


#include <stdio.h>

int sumarr(int *arr, int n)

int sum = 0;

for (int i = 0; i < n; i++)

sum += arr[i];

return sum;

int main()

int n;

printf("enter number of element required: \n");

scanf("%d", &n);

int arr[n];

int len = sizeof(arr) / sizeof(arr[0]);

printf("enter %d values \n", n);


for (int i = 0; i < n; i++)

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

for (int i = 0; i < n; i++)

printf("element %d : %d\n", i, arr[i]);

printf("The sum of all the elements in array is : %d \n",


sumarr(arr, len));

return 0;

5. Usage of Pointer to Pointer concepts


#include <stdio.h>

int main()

int a = 40;

int *ptr = &a;

int **ptv = &ptr;

printf("Value of a: %d\n", a);

printf("Value of a using pointer: %d\n", *ptr);

printf("Value of a using pointer to pointer: %d\n", **ptv);

printf("Address of a: %d\n", &a);

printf("Address of a using pointer: %d\n", ptr);

printf("Address of pointer: %d\n", &ptr);

printf("address of ptr: %d\n", ptv);


printf("value of ptr using pointer to pointer: %d\n", *ptv);

return 0;

7. String Manipulation Using Pointers


#include <stdio.h>

int main()

char str[] = "HELLO, My Name Is Krishna.";

char *ptr = str;

printf("Original string: %s\n", str);

while (*ptr != '\0')

if (*ptr >= 'A' && *ptr <= 'Z')

*ptr = *ptr + 32;

ptr++;

printf("Modified string: %s\n", str);

return 0;

}
8. Finding Maximum Element in an Array Using Pointers
#include <stdio.h>

int max_num(int *arr, int size) {

int m = arr[0];

for (int i = 1; i < size; i++) {

if (arr[i] > m) {

m = arr[i];

return m;

int main() {

int arr[] = {100, 234, 562, 987};

int size = sizeof(arr) / sizeof(arr[0]);

int ma = max_num(arr, size);

printf("%d\n", ma);

return 0;

You might also like