0% found this document useful (0 votes)
342 views7 pages

0x0e Structures and Typedef

The document defines functions to work with a struct representing a dog, including initializing, printing, creating, and freeing dogs. It includes function prototypes and implementations to initialize a dog struct with a name, age, and owner; print the values of a dog struct; dynamically allocate memory for and initialize a new dog struct; and free the memory for a dog struct. Typedefs and header files are used to clean up the code working with dog structures.

Uploaded by

ouss max
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)
342 views7 pages

0x0e Structures and Typedef

The document defines functions to work with a struct representing a dog, including initializing, printing, creating, and freeing dogs. It includes function prototypes and implementations to initialize a dog struct with a name, age, and owner; print the values of a dog struct; dynamically allocate memory for and initialize a new dog struct; and free the memory for a dog struct. Typedefs and header files are used to clean up the code working with dog structures.

Uploaded by

ouss max
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/ 7

READMEmd

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

Prototype: void print_dog(struct dog *d);


Format: see example bellow
You are allowed to use the standard library
If an element of d is NULL, print (nil) instead of this element. (if name is NULL, print
Name: (nil))
If d is NULL print nothing.

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.

Prototype: dog_t *new_dog(char *name, float age, char *owner);


You have to store a copy of name and owner
Return NULL if the function fails

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.

Prototype: void free_dog(dog_t *d);

====================================
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;

while (s[i] != '\0')


{
i++;
}

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;

while (src[len] != '\0')


{
len++;
}

for (i = 0; i < len; i++)


{
dest[i] = src[i];
}
dest[i] = '\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);

dog->name = malloc(sizeof(char) * (len1 + 1));


if (dog->name == NULL)
{
free(dog);
return (NULL);
}
dog->owner = malloc(sizeof(char) * (len2 + 1));
if (dog->owner == NULL)
{
free(dog);
free(dog->name);
return (NULL);
}
_strcpy(dog->name, name);
_strcpy(dog->owner, owner);
dog->age = age;

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);
}
}

=======================================

You might also like