iBusiness
Create a function t...
 
Share:
Notifications
Clear all

Create a function that stores the program's argument (42 Piscine, Day 8, EX05/ft_param_to_tab.c)  

  RSS

Isaac More
(@isaacmore)
Member Admin
Joined: 1 year ago
Posts: 189
19/06/2020 10:59 am  

In this exercise, you are required to create a function that stores the program's argument within an array structure and that returns the address of that array's first box. 

All elements of the array must be processed, including av[0]

Prototyped as: 

struct s_stock_par *ft_param_to_tab(int ac, char **av)

Suggested Solution

/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_param_to_tab.c                                  :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rariosa- <rariosa-@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2017/08/17 15:40:31 by rariosa-          #+#    #+#             */
/*   Updated: 2017/08/18 23:41:49 by rariosa-         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "ft_stock_par.h"

int					ft_strlen(char *c)
{
	int i;

	i = 0;
	while (c[i] != '\0')
		++i;
	return (i);
}

char				*ft_strcpy(char *dest, char *src)
{
	int i;

	i = 0;
	while (src[i] != '\0')
	{
		dest[i] = src[i];
		++i;
	}
	dest[i] = '\0';
	return (dest);
}

char				*ft_strdup(char *src)
{
	char *dest;

	dest = malloc((ft_strlen(src) + 1) * sizeof(char));
	return (dest ? ft_strcpy(dest, src) : dest);
}

struct	s_stock_par	*ft_param_to_tab(int ac, char **av)
{
	t_stock_par	*tab;
	int			i;
	int			j;

	tab = (s_stock_par *)malloc(sizeof(t_stock_par) * (ac + 1));
	if (tab)
		return (NULL);
	while (i < argc)
	{
		j = 0;
		while (av[i][j] != '\0')
			++j;
		tab[i].size_param = j;
		tab[i].str = av[i];
		tab[i].copy = ft_strdup(av[i]);
		tab[i].tab = ft_split_whitespaces(av[i]);
		++i;
	}
	tab[i].str = NULL;
	return (tab);
}

Your included ft_stock_par.h

/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_stock_par.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rariosa- <rariosa-@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/08/17 15:40:52 by rariosa- #+# #+# */ /* Updated: 2017/08/18 23:39:50 by rariosa- ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_STOCK_PAR_H # define FT_STOCK_PAR_H # include <stdlib.h> char **ft_split_whitespaces(char *str); void ft_show_tab(struct s_stock_par *par); typedef struct s_stock_par { int size_param; char *str; char *copy; char **tab; } t_stock_par; #endif

Quote
Share:

We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it. OK Read More