Pointer Question: recursive search inside byte array

Hi all. First of all sorry for this long post, but I try to explain the question at best.

I have a (const char *) to parse that contains not a string, but a byte array (it stores binary data chunk).

I have to look for the LAST bytes (last 2, last 3, etc etc) until I will find the same sequence in another part of the same bytearray. I make an example to clarify. Consider this possible (and short) byte array to parse

13 22 15 2C 91 65 54 B1 FF 91 65 54 B1 FF

the longest match possible to find is "91 65 54 B1 FF". Infact if you mentally cut the last bytes matched you would obtain:

13 22 15 FF 91 65 54 B1 FF

(so: match will be: longhest part of array with match is 5bytes long and match is located at offset 5) (note in order to count offset you must count bytes remained after "mental truncation" until the first byte of match occurs. In this case "91" of "91 65 54 B1 FF" is the 5th byte counting from end.


I hope I clarified what I am trying to do. Whell my question is about the way to do it

I wanted to use pointers like:

char * p2 = my_byte_array_to_parse + (dim_array -1);
char * p1 = p2--;
and start from those points of string.

but a friend says me (without explaination) to avoid to move pointers (he sais: pointers are like nature: don't try to force them). So I'd like your opinion


Another question related to the first one: a code like this one is correct?

char array [10] = {0,1,2,3,4,5,6,7,8,9};
int value = 4;
char * p = &array;
p += value;

if correct p should point to array[4] but I am not sure it is possible to do so.
Last edited on
You lost me in the first thing you want to achieve.

About the second one, when you declare an array, you're actually declaring a space of memory and a pointer to that space, so basically in your example "array" (without []s ) is a pointer itself, therefore you should assign it as it is:

char * p = array;

instead of

char * p = &array; // Error: cannot convert 'char (*)[10]' to 'char*' in initialization.

Small offtopic:
Those are the kind of things that can be sort out really fast with an IDE. There are people who says that it's best to start learning just with your text editor and your compiler. Well, i'm with the others. My advise to you is to start with an IDE, because that way you can correct small errors and clear up most doubts about syntax in an instant, it helps you understand errors better, and a bunch of other benefits. That makes you lose less time in meaningless things you don't want to be stuck doing, wich clears up the road for you to be able to concentrate in more important things. That helps you move forward and keeps you motivated.
This is a common CS problem. Is this homework? (If it is I'll try to help you understand it better instead of pointing to really fancy solutions.)

Also, are the sequences permitted to overlap?

As to your friend: pointers are a perfect match for this kind of problem; he doesn't know what he is talking about or he doesn't really care about helping you with your problem.

Ivan is correct about using just char * p = array;. An array without []s degenerates into a pointer to the element type.

Hope this helps.
Topic archived. No new replies allowed.