One liners for converting non-nullterminated arrays to string

H

helmuterik

I got this one to work:

#include <stdlib.h>
#include <stdio.h>

int main(void) {
char bytearray[10] = "f ";
char string[100];

snprintf(string, sizeof(string)-1, "%.*s/%.*s",
( (char *) memchr(bytearray, ' ', 10) == NULL ? 10 : (( (char *)
memchr(bytearray, ' ', 10)) - bytearray)),
bytearray,
( (char *) memchr(bytearray, ' ', 10) == NULL ? 10 : (( (char *)
memchr(bytearray, ' ', 10)) - bytearray)),
bytearray);

printf("%s\n", string);
return 0;
}

Not that I will use it in production though.
 
T

Tim Rentsch

helmuterik said:
I'd be interested in suggestions for one liners for converting two non-
nullterminated byte arrays such as "abc " and "def " (note that
both may or may not have a couple of trailing blanks) into a
nullterminated string where trailing blanks are removed, i.e. "abc/
def".

[noted later that the two arrays have at most 10 characters each]
[noted also that the two arrays have no internal blanks]

Given function parameters

char out[static 23], const char a[static 10], const char b[static 10]

it could be done by this one-line expression:

strcpy( strchr( strcat( strncpy( strcpy( strchr( strcat( strncpy( strncpy( \
out, "", 23 ), a, 10 ), " " ), ' ' ), "/" )+1, b, 10 ), " " ), ' ' ), "" \
);

with the result being in 'out'.

Of course, I never would have considered writing it this
way had it not been for the posting/request. Still it's
kind of interesting -- I wouldn't have guessed that the
str{cpy,chr,cat,ncpy} functions together (and using no
other functions) had this much power.
 
S

Stefan Ram

Tim Rentsch said:
it could be done by this one-line expression:
strcpy( strchr( strcat( strncpy( strcpy( strchr( strcat( strncpy( strncpy( \
out, "", 23 ), a, 10 ), " " ), ' ' ), "/" )+1, b, 10 ), " " ), ' ' ), "" \
);

This (

strcpy( strchr( strcat( strncpy( strcpy( strchr( strcat( strncpy( strncpy( \
out, "", 23 ), a, 10 ), " " ), ' ' ), "/" )+1, b, 10 ), " " ), ' ' ), "" \
);


) is a statement.
 
T

Tim Rentsch

This (

strcpy( strchr( strcat( strncpy( strcpy( strchr( strcat( strncpy( strncpy( \
out, "", 23 ), a, 10 ), " " ), ' ' ), "/" )+1, b, 10 ), " " ), ' ' ), "" \
);


) is a statement.

That's right. Those lines of my posting have the
aforementioned one-line expression, /and/ also
supplied at no additional cost the semicolon that
together with the expression makes a statement.
 
B

Ben Bacarisse

(very nice, by the way)
Yes, it is. You can always tell a statement by the trailing semicolon.

That's a challenge, isn't it? This is a statement with no trailing
semicolon:

if (x) {} else {}

and this has a trailing semicolon but is not a statement:

int x = 1;
 
S

Stefan Ram

Ben Bacarisse said:
That's a challenge, isn't it? This is a statement with no trailing
semicolon:
if (x) {} else {}

Yes. And

{}

would already suffice alone (as a statement not
ending with a semicolon).
and this has a trailing semicolon but is not a statement:
int x = 1;

Yes. And it is a block-item, just as a statement also is
a block-item.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,575
Members
47,207
Latest member
HelenaCani

Latest Threads

Top