Re: How to mid string on a binary string?



"jt" <jtsoft@xxxxxxxxxxx> writes:
> 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) 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: split a string
    ... But this reads a string into a single char ... Check fgetsfrom the Standard C Library as a way to get string input ... pointer to a pointer to a char. ... To handle arrays remotely. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: The type of argv in K&R2
    ... has type pointer to character strings. ... members of that array each point to the first character of a string that ... The next member, the member, ...
    (comp.lang.c)
  • Re: copy a string into a 2d array of chars
    ... This split function should allocate a 2D array of chars ... >focus the program the string is not actually split. ... later) is an array of char containing the original contents of the ... The i-th pointer will contain the starting address of the ...
    (comp.lang.c)
  • Re: problem with function
    ... > If you intended copying the string, you need to use the strcpy function. ... I suggest researching hungarian notation for variable names. ... Isn't 'psz' a pointer a an array of characters containing a string? ... pointer to char. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Help with C->C# Port
    ... char is a character. ... you would use a string in C# ... - **char is a pointer to a pointer of char. ...
    (microsoft.public.dotnet.languages.csharp)