xxxxxxxxxx
// 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 assigned to point at a struct of type airplane.
// this creates a struct of type airplane and assigns it a name of cessna and initializes it
airplane cessna = {50, 155, true};
Hummingbird = &cessna; // now assign 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;
// or
Hummingbird->landing_gear_down = false;
if (Hummingbird->landing_gear_down == false)
{
printf("\nlanding gear is up");
}
else
{
printf("\nlanding gear is down");
}
};
xxxxxxxxxx
typedef struct{
int vin;
char* make;
char* model;
int year;
double fee;
}car;
car TempCar;
tempCar->vin = 1234; // metodo 1
(*tempCar).make = "GM"; // metodo 2
tempCar->year = 1999;
tempCar->fee = 20.5;
xxxxxxxxxx
typedef struct {
char name[20];
int age;
} Info;
Info* GetData() {
Info *info;
info = (Info *) malloc( sizeof(Info) );
scanf("%s", info.name);
scanf("%d", &info.age);
return info;
}
xxxxxxxxxx
struct name {
member1;
member2;
.
.
};
int main()
{
struct name *ptr, Harry;
}