Re: General method for dynamically allocating memory for a string



"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.
.



Relevant Pages

  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)
  • Re: Insufficient guarantees for null pointers?
    ... will the compiler know what the bounds are after converting that char * ... to an int *, if it could point to either of two arrays which happen to ... compares equal to the original pointer. ...
    (comp.std.c)
  • Re: problem with memcpy and pointers/arrays confusion - again
    ... int line, unsigned long *total_mem) ... That's a long pointer address... ... If sizeof > sizeof which is ... if you allocate for char with sizeof < sizeof, ...
    (comp.lang.c)
  • Re: Request critique of first program
    ... Returns a pointer to an asplit_result struct (unless unable to ... Success is indicated by SUCCESS, ... const char *out_file_b, ... const long int num_lines); ...
    (comp.lang.c)
  • Re: Question about array declarators
    ... I found that the type of this declarator was "array of pointer to ... const int". ... type with "const int". ... The qualifiers on the type pointed at by fooare "const", the qualifiers on the type pointed at by &i are "". ...
    (comp.lang.c)