F
fatted
I'm trying to write a function which splits a string (possibly multiple
times) on a particular character and returns the strings which has been
split. What I have below is kind of (oh dear!) printing the results I
expect, which I guess means my dynamic memory allocation is a mess.
Also, I was advised previously that I should really free memory in the
same place I declare it, but I'm not sure how I would go about doing
this
in my code below.
Comments, advice and cleaner solutions welcome
--
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * mymalloc(size_t);
void * myrealloc(void *, size_t);
char ** split(char *, int);
int main(void)
{
char string[] = "this is a string = sausage";
char ** data = split(string,' ');
while(*data)
{
printf("[main]%s\n",*data++);
}
return EXIT_SUCCESS;
}
/*
** This function splits a string s, on the character c and returns a
pointer to
** an array of strings.
*/
char ** split(char * s, int c)
{
char * sptr;
int word_len;
/* according to man page on linux (hopefully standards
compliant)
char ** result = NULL;
int i = 0;
while(s)
{
if((sptr = strchr(s, c)) != NULL)
{
word_len = strlen(s) - strlen(sptr);
result = myrealloc(result,(sizeof(char
*)*(i+1)));
result = mymalloc(word_len + 1);
strncpy(result,s,word_len);
s = sptr + 1;
}
else
{
result = mymalloc(strlen(s)+1);
result = s;
s = NULL;
}
++i;
}
/* assign null to indicate end of array */
result = mymalloc(1);
result = '\0';
return(result);
}
void * myrealloc(void * ptr, size_t amount)
{
if((ptr = realloc(ptr, amount)) == NULL)
{
perror("Realloc Failed!\n");
}
return(ptr);
}
void * mymalloc(size_t amount)
{
void * ptr;
if((ptr = malloc(amount)) == NULL)
{
perror("Malloc Failed!\n");
}
return(ptr);
}
times) on a particular character and returns the strings which has been
split. What I have below is kind of (oh dear!) printing the results I
expect, which I guess means my dynamic memory allocation is a mess.
Also, I was advised previously that I should really free memory in the
same place I declare it, but I'm not sure how I would go about doing
this
in my code below.
Comments, advice and cleaner solutions welcome
--
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * mymalloc(size_t);
void * myrealloc(void *, size_t);
char ** split(char *, int);
int main(void)
{
char string[] = "this is a string = sausage";
char ** data = split(string,' ');
while(*data)
{
printf("[main]%s\n",*data++);
}
return EXIT_SUCCESS;
}
/*
** This function splits a string s, on the character c and returns a
pointer to
** an array of strings.
*/
char ** split(char * s, int c)
{
char * sptr;
int word_len;
/* according to man page on linux (hopefully standards
compliant)
char ** result = NULL;
int i = 0;
while(s)
{
if((sptr = strchr(s, c)) != NULL)
{
word_len = strlen(s) - strlen(sptr);
result = myrealloc(result,(sizeof(char
*)*(i+1)));
result = mymalloc(word_len + 1);
strncpy(result,s,word_len);
s = sptr + 1;
}
else
{
result = mymalloc(strlen(s)+1);
result = s;
s = NULL;
}
++i;
}
/* assign null to indicate end of array */
result = mymalloc(1);
result = '\0';
return(result);
}
void * myrealloc(void * ptr, size_t amount)
{
if((ptr = realloc(ptr, amount)) == NULL)
{
perror("Realloc Failed!\n");
}
return(ptr);
}
void * mymalloc(size_t amount)
{
void * ptr;
if((ptr = malloc(amount)) == NULL)
{
perror("Malloc Failed!\n");
}
return(ptr);
}