0x0e Structures and Typedef
0x0e Structures and Typedef
C - Structures, typedef TASKS 0. Poppy Define a new type struct dog with the
following elements:
name, type = char *
age, type = float
owner, type = char *
1. A dog is the only thing on earth that loves you more than you love yourself
Write a function that initialize a variable of type struct dog
Prototype: void init_dog(struct dog *d, char *name, float age, char *owner);
1. A dog will teach you unconditional love. If you can have that in your life, things
won't be too bad Write a function that prints a struct dog
1. Outside of a dog, a book is a man's best friend. Inside of a dog it's too dark to
read Define a new type dog_t as a new name for the type struct dog.
2. A door is what a dog is perpetually on the wrong side of Write a function that
creates a new dog.
1. How many legs does a dog have if you call his tail a leg? Four. Saying that a
tail is a leg doesn't make it a leg Write a function that frees dogs.
====================================
dog.h CODE
#ifndef DOG_H
#define DOG_H
/**
* struct dog - a dog's basic info
* @name: First member
* @age: Second member
* @owner: Third member
*
* Description: Longer description
*/
struct dog
{
char *name;
float age;
char *owner;
};
/**
* dog_t - typedef for struct dog
*/
typedef struct dog dog_t;
void init_dog(struct dog *d, char *name, float age, char *owner);
void print_dog(struct dog *d);
dog_t *new_dog(char *name, float age, char *owner);
void free_dog(dog_t *d);
char *_strcpy(char *dest, char *src);
int _strlen(char *s);
#endif
====================================
1-init_dog.c CODE
#include <stdlib.h>
#include "dog.h"
/**
* init_dog - initializes a variable of type struct dog
* @d: pointer to struct dog to initialize
* @name: name to initialize
* @age: age to initialize
* @owner: owner to initialize
*/
void init_dog(struct dog *d, char *name, float age, char *owner)
{
if (d == NULL)
d = malloc(sizeof(struct dog));
d->name = name;
d->age = age;
d->owner = owner;
}
====================================
2-print_dog.c CODE
#include <stdio.h>
#include <stdlib.h>
#include "dog.h"
/**
* print_dog - prints a struct dog
* @d: struct dog to print
*/
void print_dog(struct dog *d)
{
if (d == NULL)
return;
if (d->name == NULL)
d->name = "(nil)";
if (d->owner == NULL)
d->owner = "(nil)";
printf("Name: %s\nAge: %f\nOwner: %s\n", d->name, d->age,
d->owner);
}
======================================
4-new_dog.c CODE
#include <stdlib.h>
#include "dog.h"
/**
* _strlen - returns the length of a string
* @s: string to evaluate
*
* Return: the length of the string
*/
int _strlen(char *s)
{
int i;
i = 0;
return (i);
}
/**
* *_strcpy - copies the string pointed to by src
* including the terminating null byte (\0)
* to the buffer pointed to by dest
* @dest: pointer to the buffer in which we copy the string
* @src: string to be copied
*
* Return: the pointer to dest
*/
char *_strcpy(char *dest, char *src)
{
int len, i;
len = 0;
return (dest);
}
/**
* new_dog - creates a new dog
* @name: name of the dog
* @age: age of the dog
* @owner: owner of the dog
*
* Return: pointer to the new dog (Success), NULL otherwise
*/
dog_t *new_dog(char *name, float age, char *owner)
{
dog_t *dog;
int len1, len2;
len1 = _strlen(name);
len2 = _strlen(owner);
dog = malloc(sizeof(dog_t));
if (dog == NULL)
return (NULL);
return (dog);
}
=======================================
5-free_dog.c CODE
#include <stdlib.h>
#include "dog.h"
/**
* free_dog - frees memory allocated for a struct dog
* @d: struct dog to free
*/
void free_dog(dog_t *d)
{
if (d)
{
free(d->name);
free(d->owner);
free(d);
}
}
=======================================