Re: General method for dynamically allocating memory for a string
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Thu, 31 Aug 2006 04:39:46 GMT
"smnoff" <343rhinosourueus@xxxxxxxxxxx> writes:
"Frederick Gotham" <fgothamNO@xxxxxxxx> wrote in message
news:r9qJg.13289$j7.326838@xxxxxxxxxxxxxxxxx
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);
Why are there double exclamation marks in the line show above?
The assert() macro doesn't necessarily accept an argument of a type
other than int. (It does in C99, but not all compilers support C99.)
The ! (logical not) operator, applied to any scalar operand, yields
the int value 1 if the operand compares equal to 0, 0 if it doesn't.
A pointer value compares equal to 0 only if it's a null pointer. So
!p means "p is a null pointer". Applying it a second time reverses
the result, so !!p means "p is not a null pointer". !! normalizes a
scalar value, mapping 0 to 0 and anything else to 1.
So the declaration asserts that each of the pointers is non-null.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- References:
- Re: General method for dynamically allocating memory for a string
- From: Frederick Gotham
- Re: General method for dynamically allocating memory for a string
- Prev by Date: Re: strncpy not that easy to use
- Next by Date: Re: Syntax issue
- 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
|