Pointer, Structure
Pointer, Structure
1.
#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);
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;
int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;
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" };
return 0;
}
Output
String array Elements are:
Amit
Arush
Ankan
Program on Structure
1.
#include <stdio.h>
#include <string.h>
int main() {
return 0;
}
2.
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
int main()
{
s1.roll_no = 27;
strcpy(s1.name, "Kamlesh Joshi");
strcpy(s1.branch, "Computer Science And Engineering");
s1.batch = 2019;
return 0;
}
1.
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}
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);
return 0;
}
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: ");
printf("Displaying Information:\n");
for(i = 0; i < n; ++i)
printf("Name: %s\tAge: %d\n", (ptr+i)->name, (ptr+i)->age);
return 0;
}