Skip to content

Instantly share code, notes, and snippets.

@Nahiduzzaman
Last active September 7, 2018 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nahiduzzaman/e96f83ad9f671a985dace20072aad58d to your computer and use it in GitHub Desktop.
Save Nahiduzzaman/e96f83ad9f671a985dace20072aad58d to your computer and use it in GitHub Desktop.
https://www.programiz.com/c-programming/c-structures
Fact 1:
Struct Example without typedef:
struct person
{
int age;
float weight;
};
int main(){
struct person p1;
p1.age = 24;
p1.weight = 57.9;
printf("Age %d", p1.age);
}
Fact 2:
Struct Example with typedef:
#include <stdio.h>
typedef struct personData
{
int age;
float weight;
} person; // "personData" is the name of struct, "person" is alternative of struct type
// now you can use "person" to create struct object.
int main(){
person p1;
p1.age = 24;
p1.weight = 57.9;
printf("Age %d", p1.age);
}
Fact 2:
Struct Input:
#include <stdio.h>
typedef struct personData
{
int age;
float weight;
} person; // "personData" is the name of struct, "person" is alternative of struct type
// now you can use "person" to create struct object.
int main(){
person person1;
printf("Enter age : ");
scanf("%d", &person1.age);
printf("Age : %d", person1.age);
}
Fact 3
Access structur member through pointer
#include<stdio.h>
typedef struct personData
{
int age;
float weight;
} person;
int main(){
person person1, *personPtr;
person1.age = 12;
person1.weight = 56.9;
personPtr = &person1;
printf("%d \n", personPtr->age); // you use -> only when the variable is pointer
printf("%.1f", personPtr->weight);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment