Documente Academic
Documente Profesional
Documente Cultură
Structuri incluse
Uniuni
Câmpuri pe biți
Tipul enumerare
Programarea calculatoarelor I -
Gyorodi Cornelia 11
Accesarea membrilor unei structuri
Important de reținut:
Când accesăm un element al unei structuri printr-o variabilă de
structură folosim operatorul punct (.)
# include <stdio.h>
# include <string.h>
struct s_type
{
int i;
char str [80];
}s, *p;
struct
{
int a;
float f;
}var1, var2;
var1.a = 10;
var1.f = 100.25;
var2 = var1; /* var1 si var2 vor conţine aceleaşi date */
void main(void)
{
var1 = f1();
printf("%d %lf ", var1.i, var1.d);
}
tmp.i = 100;
tmp.d = 123.23;
return tmp;
}
Programarea calculatoarelor I - Gyorodi
Cornelia 16
Exemplu – Funcția care are argument pointer la structură
#include <stdio.h>
#include <string.h>
struct Books {
char title[50]; void printBook( struct Books *book )
char author[50]; {
char subject[100]; printf( "Book title : %s\n", book->title);
int book_id; }; printf( "Book author : %s\n", book->author);
/* function declaration */ printf( "Book subject : %s\n", book->subject);
void printBook( struct Books *book ); printf( "Book book_id : %d\n", book->book_id);
int main( ) }
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
} 17
Dimensiunea unei structuri
Pentru a afla dimensiunea unei structuri, va trebui să folosiţi operatorul
sizeof.
int i;
i = sizeof (struct persoana);
printf ("persoana are lungimea de %d bytes", i);
#define MAXNAME 30
#define MAXPERSONS 2
struct date {
unsigned char day;
unsigned char month;
int year;
};
struct person {
char firstName[MAXNAME];
char lastName[MAXNAME];
struct date dateOfBirth;
};
clrscr();
/* Read in the person's data */
for(i = 0; i < MAXPERSONS; i++) {
printf("Please enter the details of person %d.\n", i+1);
printf("\tLast name: "); gets(p[i].lastName);
printf("\tFirst name: "); gets(p[i].firstName);
printf("\tDay of birth: "); scanf("%uc", &p[i].dateOfBirth.day);
printf("\tMonth of birth: "); scanf("%uc", &p[i].dateOfBirth.month);
printf("\tYear of birth: "); scanf("%d", &p[i].dateOfBirth.year);
getchar(); /* ultimul scanf nu citeste \n, asa ca noi trebuie sa il citim
explicit ! */
}
union number {
int x;
double y;
};
int main() {
union number value;
clrscr();
value.x = 100;
printf( "%s\n%s\n%s%d\n%s%f\n\n","Atribuie campului de tip int o valoare",
"si afiseaza ambele campuri.","int: ", value.x, "double:\n", value.y );
return EXIT_SUCCESS;
}
Mai multe detalii în Bibl 1 - Exemplu 7.2
struct b_type {
unsigned department:3; /*până la 7 departamente */
unsigned instoc:1; /*1 dacă există stoc, 0 dacă nu */
unsigned time:3; /* perioada în luni */
unsigned comandat:1; /* 1 comandat, 0 nu */
}tab[max];
În acest caz, într-un singur octet se pot stoca informaţii pentru fiecare
articol care în mod normal (dacă nu am folosi cîmpul de biţi), ar ocupa 4
octeţi.
De exemplu:
tab [3].departament = 4; /* asignează valoarea 4 câmpului
departament al articolului 4 */
# include <stdio.h>
# include <conio.h>
struct exemplu
{
unsigned a :1;
unsigned b :1;
unsigned c :1;
unsigned d :1;
unsigned e :1;
unsigned f :1;
unsigned g :1;
unsigned h :1;
};
De exemplu :
enum culori { rosu, galben, verde } col;
enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
int main()
{
enum months month;
const char *monthName[] = { "", "January", "February", "March", "April",
"May","June", "July", "August", "September", "October", "November", "December"};
for ( month = JAN; month <= DEC; month=month+1 )
printf( "%2d%11s\n", month, monthName[ month ] );
return EXIT_SUCCESS;
}
37
Exemplu – definire de tipuri de date
#include <stdio.h>
#define MAXMODEL 32
#define MAXREGNO 10
#define MAXCARS 20
struct car {
char model[MAXMODEL];
char regno[MAXREGNO];
int year;
int capacity;
};