jt said:
I'm needing to take a binary string start at a certain position and
return a pointer from that postion to the end of the binary stirng.
something like this:
char bstr[2048];
char *pos;
pos=mid(bstr,35); / *return a pointer of the rest of the binary string
starting at element 35 */
Any help showing me how I can do this? I don't know how I can do a char
*mid(bstr,ele)
It's not clear (to me, anyway) just what you mean by "binary string".
A C string is "a contiguous sequence of characters terminated by and
including the first null character".
Presumably in a "binary string" you want to allow null characters
within the string -- which means you have to have some other way to
specify how long it is. You don't seem to have done so here.
Also, if you want to store arbitrary bit patterns, you'll be better
off using unsigned char rather than char.
If you're just talking about ordinary strings, you can ignore most of
the above.
The usual way of manipulating a string is via a pointer (of type
char*) to its first element. The pointer only specifies where the
first character is; it works as a pointer to the whole string because
the end is marked by the null character.
So, ignoring the question of determining where it ends, here's how
to get a pointer to element 35 (which is actually the 36th element)
of bstr:
char bstr[2048];
char *pos = bstr + 35;
If you're uncomfortable with C's pointer arithmetic, you can also do
this as:
char bstr[2048];
char *pos = &bstr[35];
This is equivalent because the indexing operator is defined in terms
of pointer arithmetic: x[y] is by definition equivalent to *(x+y), and
the unary "*" and "&" operators cancel each other out. (Usually x is
a pointer and y is an integer.)
Here's an example:
#include <stdio.h>
int main(void)
{
char *s = "hello, world";
printf("s = \"%s\"\n", s);
printf("s+7 = \"%s\"\n", s+7);
printf("&s[7] = \"%s\"\n", &s[7]);
return 0;
}
Output:
s = "hello, world"
s+7 = "world"
&s[7] = "world"
Read section 6, "Arrays and Pointers", of the C FAQ (google "C FAQ").
Then read the rest of it.
--
Keith Thompson (The_Other_Keith) (e-mail address removed)
<
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.