c - Using write() to print to console -
so trying create function uses write() scheme phone call (printf , other options not available) output string console. however, nail snag.
here function:
static void doprint (const char *s) { write (stdout_fileno, s, sizeof(s)); }
which beingness called by:
doprint ("hello world!\n");
however, prints out:
hello wo
what doing wrong?
note: access stdlib , stdio restricted
thanks
in context sizeof
doesn't want - yields size of pointer, not size of string. utilize strlen
instead.
but access stdlib restricted
you can roll own using while
, counter. untested:
size_t my_strlen(const char *s) { size_t len = 0; while (*s++) len++; homecoming len; }
or more efficient version:
size_t my_strlen(const char* s) { const char *end = s; while (*end++) {}; homecoming end-s-1; }
the above works because pointer advanced lastly position in string , size difference between lastly pointer location , first pointer location, minus 1 of course of study handle null-terminating character.
c
No comments:
Post a Comment