![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
If you like, you can write another simple function, company_name
,
that returns a pointer to the portion of the description that contains the
company-identifying characters.
Because the company name occupies the final portion of the description string, there is no need to create a new character array. Instead, you can view the final portion of the description string as a substring.
Thus, the company_name
function can do its job by locating the
character in the description string immediately following the hyphen,
whereupon the company_name
function can return the address of that
character. That address is the address of a substring containing the
company-identifying characters. In the following, for example,
company_name
is to return 922
:
description *--------*--------* |00000011|10011000| *--------*--------* ----------------- | *-------* | | C - I B M Null character | | | | | | | v v v v v v v -------- -------- -------- -------- -------- -------- ----*--------*--------*--------*--------*--------*--------*- |01000011|00101101|01001001|01000010|01001101|00000000| ---*--------*--------*--------*--------*--------*--------*--- 920 921 922 923 924 925
The following is one interesting way to implement company_name
.
Note that nothing needs to be initialized in the for
statement; hence,
nothing appears before the first semicolon. Also note that the iteration
terminates when the cptr
pointer points to the hyphen; hence, the
pointer has to be incremented once after leaving the for
loop so as
to move the pointer forward to the first character in the company name:
char* company_name (struct trade *t) { char* cptr = t -> for; description (; cptr[0] != '-'; ++cptr) ; return ++cptr; }