Frederick's code is hard to read and harder to learn from. Here is
some fully checked code with lots of comments.
///////////////////////////////////////////////////////////////////////////////
// file: substr.c
// Note: C++ style comments are allowed for C99 compliant compilers.
///////////////////////////////////////////////////////////////////////////////
#include <stdlib.h> // for malloc() and free()
#include <string.h> // for strncpy()
#include <sys/types.h> // for size_t
#include <stdio.h> // for printf()
char * substr( char * string, size_t start, size_t end);
int main( void ) {
char *str1 = substr("abcdefghijklmnop",2,7);
char *str2 = substr("abcdefghijklmnop",4,15);
if( str1 != NULL ) {
printf( "str1: %s\n", str1);
} else {
// Setting a null pointer to zero ensures you
// can delete it more than once (free) without
// undefined behavior. This is a good
// programming habit.
str1 = 0;
}
if( str2 != NULL ) {
printf( "str2: %s\n", str2 );
} else {
// Setting a null pointer to zero ensures you
// can delete it more than once (free) without
// undefined behavior. This is a good
// programming habit.
str2 = 0;
}
free( str1 );
free( str2 );
return 0;
}
/**
* Note: this function will return a newly allocated string. It
* is your responsibility to delete this memory to prevent a leak.
*
* param "string" - the string you want to extract a substring from.
* param "start" - the array index to begin your substring.
* param "start" - the array index to begin your substring.
* param "end" - the array index to terminate your substring.
*
* On Error: this function returns null;
*/
char * substr( char * string, size_t start, size_t end) {
// pointer to the substring on the heap
char *subString;
// calculate the total amount of memory needed
// to hold the substring.
// Algo: end - start + null terminator
size_t subStringSize = end - start + 1;
// request enough bytes to store the entire
// substring and null terminator.
subString = malloc( subStringSize );
// test to make sure we got the memory
// from malloc
if( subString != NULL ) {
// Note this copies one extra byte (the
// null terminator's spot) which is garbage.
// We have to terminate this string.
strncpy( subString, string + start, subStringSize );
subString[subStringSize] = '\0';
}
// This will either be NULL if we didn't get the
// memory from malloc or it will have our substring.
return subString;
} // end function substr
I hope this helps you.
-Randall
Frederick said:
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();
}