I would like to add that pointers can only add/subtract the number of element from it's type example: int *pint=[0,2,5,3]; pint = pint + 2;//would now point to element [5] John Chung --- Rikard Bosnjakovic wrote: > On 18/04/2008, James Salisbury > wrote: > > > Hi, > > > > Does anyone have a good page and some exercises > on the use of pointers? > > I am trying to find out why the code below won't > compile line 244 I get > > invalid operand and failed to generate > expression. > > > > Thanks > > I'm a bit tipsy at the moment, but I will try to do > my best in explaining. > > The reason you can't do that is because pointers > can't, shouldn't and > musn't be treated as integers. This is heavily > pointed out(!) in K&R. > Say, for example, pnd_point points at something in > page 1 and > dec_point points to something in page 42, > subtracting them both would > return something completely uncomprehendible. For > your case this isn't > the situation, as you get the address of two > particular characters in > the string, but the compiler doesn't know about > this. > > The solution for your problem is a loop where you > compare your > strchr()-pointer to the string-pointer, subtract a > variable until they > match to get the amount of bytes. And the same for > the other pointer. > Then you will have two integers, which you can > substract. > > Something like thids: > > char *p_tmp > int substraction; > for (i = 0, p_tmp = pnd_point; p_tmp-- != rx_array; > i++) {} > for (j = 0, p_tmp = dec_point; p_tmp-- != rx_array; > j++) {} > substraction = i - j; > > Untested, but hopefully you will get the idea. > > The important part is that you have to unlearn(?) > the fact that you > cannot add or substract different pointers, only > increment/decrement > the pointers themselves. > > > -- > - Rikard - http://bos.hack.org/cv/ > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > ____________________________________________________________________________________ Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist