pointer sainty check

R

ram

In this complete tiny example, version one of an expression fails

----> theChar = *(currentMessage + characterNumber);

while my workaround expression version two succeeds.

----> theChar = (message[messageNumber])[characterNumber];

Both versions are fine on one C compiler and target but on another
version one fails. My understanding is that there should be no practical
difference, i.e. both should work.

- rob

# include <stdio.h>

const unsigned char *message[3] = {
"one message",
"second message",
"third and last maessage",
};

unsigned char *currentMessage;
unsigned char theChar;

unsigned char messageNumber, characterNumber;

unsigned char result;

int main(void)
{
result = 0;

/* version one */

currentMessage = (unsigned char *) message[1];
characterNumber = 2;

theChar = *(currentMessage + characterNumber);

if ('c' == theChar)
result |= 1;

/* version two */

messageNumber = 1;
characterNumber = 2;

theChar = (message[messageNumber])[characterNumber];

if ('c' == theChar)
result |= 2;

/* report */

printf("\n%d\n", result); /* should be 3, but is in fact 2 */
}
 
J

jacob navia

ram said:
In this complete tiny example, version one of an expression fails

----> theChar = *(currentMessage + characterNumber);

while my workaround expression version two succeeds.

----> theChar = (message[messageNumber])[characterNumber];

Both versions are fine on one C compiler and target but on another
version one fails. My understanding is that there should be no practical
difference, i.e. both should work.

- rob

# include <stdio.h>

const unsigned char *message[3] = {
"one message",
"second message",
"third and last maessage",
};

unsigned char *currentMessage;
unsigned char theChar;

unsigned char messageNumber, characterNumber;

unsigned char result;

int main(void)
{
result = 0;

/* version one */

currentMessage = (unsigned char *) message[1];
characterNumber = 2;

theChar = *(currentMessage + characterNumber);

if ('c' == theChar)
result |= 1;

/* version two */

messageNumber = 1;
characterNumber = 2;

theChar = (message[messageNumber])[characterNumber];

if ('c' == theChar)
result |= 2;

/* report */

printf("\n%d\n", result); /* should be 3, but is in fact 2 */
}

Using lcc-win32 I obtain 3...
 
A

Arthur J. O'Dwyer

In this complete tiny example [snipped], version one of an expression
fails while my workaround expression version two succeeds.

Both versions are fine on one C compiler and target but on another version
one fails. My understanding is that there should be no practical difference,
i.e. both should work.

That's my understanding also. However, the cast to 'unsigned char*'
is unnecessary and potentially dangerous once you start revising the
code; I suggest you remove it and see whether that magically fixes the
problem with the broken compiler. IOW, change
currentMessage = (unsigned char *) message[1];
to

currentMessage = message[1];

and change the definition of 'currentMessage' to
const unsigned char *currentMessage;

If that doesn't fix the problem, then I'd dismiss that compiler as
hopelessly broken --- unless of course someone else can point out
where you and I went wrong in our analyses!

HTH,
-Arthur
 
R

ram

Arthur J. O'Dwyer wrote:

That's my understanding also. However, the cast to 'unsigned char*'
is unnecessary and potentially dangerous once you start revising the
code; I suggest you remove it and see whether that magically fixes the
problem with the broken compiler. IOW, change
currentMessage = (unsigned char *) message[1];

to

currentMessage = message[1];


and change the definition of 'currentMessage' to
const unsigned char *currentMessage;

If that doesn't fix the problem, then I'd dismiss that compiler as
hopelessly broken --- unless of course someone else can point out
where you and I went wrong in our analyses!

Yes, that is exactly what I should have done and I will now spend some
time developing an understanding of this matter. Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top