0% found this document useful (0 votes)
25 views2 pages

Exemplu Rezolvat Struct

The document describes a C program that defines a struct called Person to store name, age, height, and weight. It includes functions to create, print, and destroy Person objects. The main function demonstrates creating two Person objects, printing their details, updating their ages/weights, and printing them again before destroying the objects. It also shows using assert to validate a divisor is not zero before dividing two inputs.

Uploaded by

aghinitza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Exemplu Rezolvat Struct

The document describes a C program that defines a struct called Person to store name, age, height, and weight. It includes functions to create, print, and destroy Person objects. The main function demonstrates creating two Person objects, printing their details, updating their ages/weights, and printing them again before destroying the objects. It also shows using assert to validate a divisor is not zero before dividing two inputs.

Uploaded by

aghinitza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Exemplu de folosire a tipului structur (explicaii i demonstraie live, la

curs) preluat din http://c.learncodethehardway.org/book/ex16.html


#include
#include
#include
#include

<stdio.h>
<assert.h>
<stdlib.h>
<string.h>

struct Person {
char *name;
int age;
int height;
int weight;
};
struct Person *Person_create(char *name, int age, int height, int weight)
{
struct Person *who = malloc(sizeof(struct Person));
assert(who != NULL);
who->name = strdup(name);
who->age = age;
who->height = height;
who->weight = weight;
return who;
}
void Person_destroy(struct Person *who)
{
assert(who != NULL);
free(who->name);
free(who);
}
void Person_print(struct Person *who)
{
printf("Name: %s\n", who->name);
printf("\tAge: %d\n", who->age);
printf("\tHeight: %d\n", who->height);
printf("\tWeight: %d\n", who->weight);
}
int main(int argc, char *argv[])
{
// make two people structures
struct Person *joe = Person_create(
"Joe Alex", 32, 64, 140);
struct Person *frank = Person_create(
"Frank Blank", 20, 72, 180);
// print them out and where they are in memory
printf("Joe is at memory location %p:\n", joe);
Person_print(joe);
printf("Frank is at memory location %p:\n", frank);
Person_print(frank);

// make everyone age 20 years and print them again


joe->age += 20;
joe->height -= 2;
joe->weight += 40;
Person_print(joe);
frank->age += 20;
frank->weight += 20;
Person_print(frank);
// destroy them both so we clean up
Person_destroy(joe);
Person_destroy(frank);
return 0;
}

Exemplu simplu pentru inelegerea rolului funciei assert() - (explicaii i


demonstraie live, la curs) preluat din
http://www.programmingsimplified.com/c/source-code/assert
#include <stdio.h>
#include <assert.h>
int main()
{ int a, b;
printf("Input two integers to divide\n");
scanf("%d%d", &a, &b);
assert(b != 0);
printf("%d/%d = %.2f\n", a, b, a/(float)b);
return 0;
}

You might also like