Pointer To A Structure In C

[Solved] Pointer To A Structure In C | C - Code Explorer | yomemimo.com
Question : pointer to a structure in c

Answered by : barry-cox

// setting a pointer to a struct in C
// and set value by pointer
#include <stdio.h>
#include <stdbool.h>
// a struct called airplane
typedef struct
{ int fuel; double speed; bool landing_gear_down;
} airplane;
int main()
{ // make a pointer airplane *Hummingbird; // *Hummingbird is now a pointer and it can be asigned to point at a struct of type airplane. // this creates a struct of type airplane and asigns it a name of cessna and initializes it airplane cessna = {50, 155, true}; Hummingbird = &cessna; // now asign the pointer to the struct address // you can now call data types / variables via the pointer (*Hummingbird).fuel; // this is a little messy Hummingbird->speed; // this is clean and understandable printf("\nthe cessna has %d liters of fuel", Hummingbird->fuel); // set the value by pointer Hummingbird->fuel = 15; printf("\nthe cessna has %d liters of fuel", Hummingbird->fuel); (*Hummingbird).landing_gear_down = false; if (Hummingbird->landing_gear_down == false) { printf("\nlanding gear is up"); } else { printf("\nlanding gear is down"); }
};

Source : | Last Update : Fri, 15 Apr 22

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

Answers related to pointer to a structure in c

Code Explorer Popular Question For C