Re: General method for dynamically allocating memory for a string
- From: Frederick Gotham <fgothamNO@xxxxxxxx>
- Date: Thu, 31 Aug 2006 00:35:35 GMT
smnoff posted:
I have searched the internet for malloc and dynamic malloc; however, I
still don't know or readily see what is general way to allocate memory
to char * variable that I want to assign the substring that I found
inside of a string.
Any ideas?
Unchecked code, may contain an error or two:
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
char *to_release;
void ReleaseLastString(void)
{
free(to_release);
}
char const *CreateSubstring(char const *const p,
size_t const istart,size_t const iend)
{
int assert_dummy = (assert(!!p),assert(!!istart),assert(!!iend),0);
if(!p[iend+1]) return to_release = 0, p + istart;
to_release = malloc(iend - istart + 1);
memcpy(to_release,p,iend - istart);
to_release[iend - istart] = 0;
return to_release;
}
int main()
{
puts(CreateSubstring("abcdefghijklmnop",2,7));
ReleaseLastString();
puts(CreateSubstring("abcdefghijklmnop",4,15));
ReleaseLastString();
}
--
Frederick Gotham
.
- Follow-Ups:
- Prev by Date: Re: little endian , big endian , big headache
- Next by Date: Re: A newbie's code
- Previous by thread: Re: General method for dynamically allocating memory for a string
- Next by thread: Re: General method for dynamically allocating memory for a string
- Index(es):
Relevant Pages
|