Link to home
Start Free TrialLog in
Avatar of pimpernel
pimpernel

asked on

What's the difference between these two ??

Hi, what is the difference between these two codefragments ??
I don't see any difference...
**** THIS WORKS ****
int16* dst=p1->buffer;  //buffer consists allso out of int16's
for (int i = 0; i < strlen(aa); i++)
      {
      val = *aa;
      SSVAL(dst, 0, val);
      dst++;
      aa++;
      if (val == 0)
            break;
      }
**** END ****

**** THIS DOESN'T WORKS ****

for (int i = 0; i < strlen(aa); i++)
      {
      val = *aa;
      SSVAL(p1->buffer, 0, val);
      p1->(buffer++);
      aa++;
      if (val == 0)
            break;
      }
****END****

Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image


From your comment and the code fragments, it appears that p1 is defined as:

typedef struct
{
...
  int16 buffer[SomeLength];
...
} p1_type;

p1_type *p1;

In your first example you assign the address of p1->buffer to dst and then increment dst each time through your loop.

In your second example you increment p1->buffer[0] each time through the loop.


Kent
Avatar of pimpernel
pimpernel

ASKER

Thanks. But how should I increment it then correct (like in the first example...) ??

ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, Kdo, I'll keep the first sollution...
Thanks guntherothk for the tip. I'll put the strlen out of the "for".

Yesterday i answered to that question

     http:Q_21154939.html

It was using a ToUnic function that had identical code fragments than your code, but hadn't the bugs kdo and gunther already pointed out. Obviously you are trying to change working code, what isn't recommended even if you would exactly know what you are doing.

As you made no response to the comments of kdo and gunther, i'll explain again the errors you made:

With p1->(buffer++) you are changing the pointer value of the data member 'buffer' in the struct _UNICODE_STRING. The instance 'p1' of  that struct has an invalid buffer member then (it points to the terminating zero and *not* to the begin of the previously allocated storage).

The main difference to the old working code is, that it uses a local pointer variable, thus leaving the value of p1->buffer intact.

The second error was similar: Instead of using a local variable 'len' as upper bound of the for loop, you are using strlen function. However, the argument of strlen is the char pointer 'aa' that get changed in the loop when that statement executes: aa++; . So, strlen(aa) decrements and i++ increments, e. g. with a string length of 10 only 5 bytes were copied to the UNICODE string.

Regards, Alex








Hello,
sorry for my very late response, but I don't check my mail very often...
Thanks for your extra explanation.
I allreasy understood the remark of Kdo, but indeed didn't entirely understood guntherothk's remark (since the first code with the strlen also worked).

Now it's entirely clear (I think), and since my strings allways have a fixed length I just replaced the strlen part with th length.

Greetz, pimpernel