Question : struct pointer c
Answered by : evil-emu-p6j8su8sfli5
#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;
}
Source : https://www.programiz.com/c-programming/c-structures-pointers | Last Update : Thu, 30 Dec 21
Question : c structure with pointer
Answered by : praveen-ivdhvt7g47nt
#include<stdio.h>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {1, 2};
// p2 is a pointer to structure p1
struct Point *p2 = &p1;
// Accessing structure members using structure pointer
printf("%d %d", p2->x, p2->y);
return 0;
}
Source : https://www.geeksforgeeks.org/structures-c/ | Last Update : Sun, 26 Jun 22