Skip to content

Commit bc8b1c9

Browse files
committed
added: 0-create_array.c
1 parent cb87b3a commit bc8b1c9

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

0x0B-malloc_free/0-create_array.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
/**
6+
* create_array - creates an array of chars, and initializes it with
7+
* a specific char.
8+
*
9+
* @size: size of the array.
10+
* @character: char to fill the array with.
11+
*
12+
* Return: pointer to the allocated memory, or NULL if something happen.
13+
*/
14+
char *create_array(unsigned int size, char character)
15+
{
16+
unsigned int index;
17+
char *ptr;
18+
19+
if (size == 0)
20+
return (NULL);
21+
22+
ptr = malloc(size * sizeof(char));
23+
24+
if (ptr == NULL)
25+
return (NULL);
26+
27+
28+
for (index = 0; index < size; index++)
29+
ptr[index] = character;
30+
31+
return (ptr);
32+
}

0x0B-malloc_free/0-main.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "main.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
/**
6+
* simple_print_buffer - prints buffer in hexa
7+
* @buffer: the address of memory to print
8+
* @size: the size of the memory to print
9+
*
10+
* Return: Nothing.
11+
*/
12+
void simple_print_buffer(char *buffer, unsigned int size)
13+
{
14+
unsigned int i;
15+
16+
i = 0;
17+
while (i < size)
18+
{
19+
if (i % 10)
20+
{
21+
printf(" ");
22+
}
23+
if (!(i % 10) && i)
24+
{
25+
printf("\n");
26+
}
27+
printf("0x%02x", buffer[i]);
28+
i++;
29+
}
30+
printf("\n");
31+
}
32+
33+
/**
34+
* main - check the code for ALX School students.
35+
*
36+
* Return: Always 0.
37+
*/
38+
int main(void)
39+
{
40+
char *buffer;
41+
42+
buffer = create_array(98, 'H');
43+
if (buffer == NULL)
44+
{
45+
printf("failed to allocate memory\n");
46+
return (1);
47+
}
48+
simple_print_buffer(buffer, 98);
49+
free(buffer);
50+
return (0);
51+
}

0x0B-malloc_free/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# 0x0B-malloc_free

0x0B-malloc_free/_putchar.c

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <unistd.h>
2+
3+
/**
4+
* _putchar - writes the character c to stdout
5+
* @c: The character to print
6+
*
7+
* Return: On success 1.
8+
* On error, -1 is returned, and errno is set appropriately.
9+
*/
10+
int _putchar(char c)
11+
{
12+
return (write(1, &c, 1));
13+
}

0x0B-malloc_free/main.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _MAIN_H_
2+
#define _MAIN_H_
3+
4+
int _putchar(char c);
5+
char *create_array(unsigned int, char);
6+
7+
#endif /* _MAIN_H_ */

0 commit comments

Comments
 (0)