C 101: Creating Structures 
Method 1:
// define it as a template
struct person {
char first_name[50];
char last_name[50];
};

// create two instances of person
struct person Person1, Person2;

// assign values to Person1
strcpy(Person1.first_name, "Angel");
strcpy(Person1.last_name, "Cool");

Method 2:
// define it and create two instances of it
struct car {
char make[50];
char color[50];
} Car1, Car2;

// assign values to Car1
strcpy(Car1.make, "Chevy");
strcpy(Car1.color, "red");

Method 3:
// use 'typedef' to create it
typedef struct {
char first_name[50];
char last_name[50];
} person;

// create two instances of person (no 'struct' keyword needed)
person Person1, Person2;

Method 1 example
[acool@localhost C-practice]$ 
[acool@localhost C-practice]$ cat method_1.c
#include <stdio.h>
#include <string.h>

void main(){
struct person {
char first_name[50];
char last_name[50];
};

// create two instances of person
struct person Person1, Person2;

// assign values to Person1
strcpy(Person1.first_name, "Angel");
strcpy(Person1.last_name, "Cool");

// print assigned values
printf("Hello %s %s!\n", Person1.first_name, Person1.last_name);
}
[acool@localhost C-practice]$ gcc method_1.c -o method_1
[acool@localhost C-practice]$ ./method_1
Hello Angel Cool!
[acool@localhost C-practice]$

Method 2 example
[acool@localhost C-practice]$ 
[acool@localhost C-practice]$ cat method_2.c
#include <stdio.h>
#include <string.h>

void main(){
// define it and create two instances of it
struct car {
char make[50];
char color[50];
} Car1, Car2;

// assign values to Car1
strcpy(Car1.make, "Chevy");
strcpy(Car1.color, "red");

// print assigned values
printf("My %s %s.\n",Car1.color, Car1.make);
}
[acool@localhost C-practice]$
[acool@localhost C-practice]$ gcc method_2.c -o method_2
[acool@localhost C-practice]$ ./method_2
My red Chevy.
[acool@localhost C-practice]$

Method 3 example
[acool@localhost C-practice]$ 
[acool@localhost C-practice]$ cat method_3.c
#include <stdio.h>
#include <string.h>

void main(){
// use 'typedef' to create it
typedef struct {
char first_name[50];
char last_name[50];
} person;

// create two instances of person (no 'struct' keyword needed)
person Person1, Person2;

// assign values to Person1
strcpy(Person1.first_name, "Andres Manuel");
strcpy(Person1.last_name, "Lopez-Obrador");

printf("Viva la 4T! Viva %s %s!\n", Person1.first_name, Person1.last_name);
}
[acool@localhost C-practice]$
[acool@localhost C-practice]$ gcc method_3.c -o method_3
[acool@localhost C-practice]$ ./method_3
Viva la 4T! Viva Andres Manuel Lopez-Obrador!
[acool@localhost C-practice]$
[acool@localhost C-practice]$


Another example:
[acool@localhost ~]$ 
[acool@localhost ~]$
[acool@localhost ~]$ date
Wed Apr 5 02:18:30 PM PDT 2023
[acool@localhost ~]$
[acool@localhost ~]$ cat books.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Books {
char title[50];
char author[50];
char subject[100];
int id;
};

typedef struct {
char title[50];
char author[50];
char subject[100];
int id;
} eBooks;

void printdetails(eBooks *myEbook, struct Books *myBook)
{
printf( "myBook id : %d\n", myBook->id);
printf( "myBook title : %s\n", myBook->title);
printf( "myBook author : %s\n", myBook->author);
printf( "myBook subject : %s\n", myBook->subject);

printf("====================================\n");

printf( "myEbook id : %d\n", myEbook->id);
printf( "myEbook title : %s\n", myEbook->title);
printf( "myEbook author : %s\n", myEbook->author);
printf( "myEbook subject : %s\n", myEbook->subject);
}

int main()
{
// Pay attention to this! ...two different ways to declare it.
struct Books *myBook = (struct Books *) malloc(sizeof(struct Books));
eBooks *myEbook = (eBooks *) malloc(sizeof(eBooks));

strcpy( myBook->title, "C Programming");
strcpy( myBook->author, "Angel Cool");
strcpy( myBook->subject, "Programming C Structs Examples");
myBook->id = 1;

strcpy( myEbook->title, "C Programming (eBook)");
strcpy( myEbook->author, "Angel Cool (eBook)");
strcpy( myEbook->subject, "Programming C Structs Examples (eBook)");
myEbook->id = 2;

printdetails(myEbook,myBook);

return 0;
}
[acool@localhost ~]$
[acool@localhost ~]$
[acool@localhost ~]$ gcc books.c -o books
[acool@localhost ~]$
[acool@localhost ~]$ ./books
myBook id : 1
myBook title : C Programming
myBook author : Angel Cool
myBook subject : Programming C Structs Examples
====================================
myEbook id : 2
myEbook title : C Programming (eBook)
myEbook author : Angel Cool (eBook)
myEbook subject : Programming C Structs Examples (eBook)
[acool@localhost ~]$
[acool@localhost ~]$ file books
books: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=ea1090588d4c761da3060066a7d7a67b7db9caa6, for GNU/Linux 3.2.0, not stripped
[acool@localhost ~]$
[acool@localhost ~]$


Comments
Comments are not available for this entry.
2024 By Angel Cool