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

Pointer, Structure

Pointers in c

Uploaded by

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

Pointer, Structure

Pointers in c

Uploaded by

Bishnu gopal Das
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Program on Pointer

1.

#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);

// Notice the use of & before var


printf("address of var: %p", &var);
return 0;
}
output
var: 5
address of var: 2686778

2.

int* pc, c;
c = 5;
pc = &c;
printf("%d", *pc); // Output: 5

3.

int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1

4.

int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1

5.

int* pc, c, d;
c = 5;
d = -15;

pc = &c; printf("%d", *pc); // Output: 5


pc = &d; printf("%d", *pc); // Ouptut: -15
Program on Array of Pointer
// C program to demonstrate the use of array of pointers
#include <stdio.h>

int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;

// array of pointers to integers


int* ptr_arr[3] = { &var1, &var2, &var3 };

// traversing using loop


for (int i = 0; i < 3; i++) {
printf("Value of var%d: %d\tAddress: %p\n", i + 1, *ptr_arr[i],
ptr_arr[i]);
}

return 0;
}
Array of Pointers to Character

One of the main applications of the array of pointers is to store multiple strings as an array of
pointers to characters. Here, each pointer in the array is a character pointer that points to the first
character of the string.

char* arr[5]
= { "gfg", "geek", "Geek", "Geeks", "GeeksforGeeks" }

1.
// C Program to print Array of strings without array of pointers
#include <stdio.h>
int main()
{
char str[3][10] = { "Amit", "Arush", "Ankan" };

printf("String array Elements are:\n");

for (int i = 0; i < 3; i++) {


printf("%s\n", str[i]);
}

return 0;
}

Output
String array Elements are:
Amit
Arush
Ankan
Program on Structure
1.

#include <stdio.h>
#include <string.h>

// create struct with person1 variable


struct Person {
char name[50];
int citNo;
float salary;
} person1;

int main() {

// assign value to name of person1


strcpy(person1.name, "Amit Kumar");

// assign values to other person1 variables


person1.citNo = 1234;
person1. salary = 10,000;

// print struct variables


printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f", person1.salary);

return 0;
}

2.

// C Program to demonstrate Structure pointer


#include <stdio.h>
#include <string.h>

struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};

int main()
{

struct Student s1;


struct Student* ptr = &s1;

s1.roll_no = 27;
strcpy(s1.name, "Kamlesh Joshi");
strcpy(s1.branch, "Computer Science And Engineering");
s1.batch = 2019;

printf("Roll Number: %d\n", (*ptr).roll_no);


printf("Name: %s\n", (*ptr).name);
printf("Branch: %s\n", (*ptr).branch);
printf("Batch: %d", (*ptr).batch);

return 0;
}

Program on structure pointer


A structure pointer is defined as the pointer which points to the address of the memory block that
stores a structure known as the structure pointer. Complex data structures like Linked lists, trees,
graphs, etc. are created with the help of structure pointers. The structure pointer tells the address of a
structure in memory by pointing the variable to the structure variable.

1.

#include <stdio.h>
struct person
{
int age;
float weight;
};

int main()
{
struct person *personPtr, person1;
personPtr = &person1;

printf("Enter age: ");


scanf("%d", &personPtr->age);

printf("Enter weight: ");


scanf("%f", &personPtr->weight);

printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);

return 0;
}

Using arrow operator

// C Program to demonstrate Structure pointer


#include <stdio.h>
#include <string.h>
// Creating Structure Student
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};

// variable of structure with pointer defined


struct Student s, *ptr;

int main()
{

ptr = &s;
// Taking inputs
printf("Enter the Roll Number of Student\n");
scanf("%d", &ptr->roll_no);
printf("Enter Name of Student\n");
scanf("%s", &ptr->name);
printf("Enter Branch of Student\n");
scanf("%s", &ptr->branch);
printf("Enter batch of Student\n");
scanf("%d", &ptr->batch);

// Displaying details of the student


printf("\nStudent details are: \n");

printf("Roll No: %d\n", ptr->roll_no);


printf("Name: %s\n", ptr->name);
printf("Branch: %s\n", ptr->branch);
printf("Batch: %d\n", ptr->batch);

return 0;
}

Dynamic memory allocation of structure

1.

#include <stdio.h>
#include <stdlib.h>
struct person {
int age;
float weight;
char name[30];
};
int main()
{
struct person *ptr;
int i, n;
printf("Enter the number of persons: ");
scanf("%d", &n);
// allocating memory for n numbers of struct person
ptr = (struct person*) malloc(n * sizeof(struct person));
for(i = 0; i < n; ++i)
{
printf("Enter first name and age respectively: ");

// To access members of 1st struct person,


// ptr->name and ptr->age is used

// To access members of 2nd struct person,


// (ptr+1)->name and (ptr+1)->age is used
scanf("%s %d", (ptr+i)->name, &(ptr+i)->age);
}

printf("Displaying Information:\n");
for(i = 0; i < n; ++i)
printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age);

return 0;
}

You might also like