473,386 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointer sainty check

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 */
}
Nov 14 '05 #1
3 1088
ram wrote:
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...
Nov 14 '05 #2

On Mon, 7 Feb 2005, ram wrote:

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
Nov 14 '05 #3
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!

--
rob

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: tthunder | last post by:
Hi @all, Perhaps some of you know my problem, but I had to start a new thread. The old one started to become very very confusing. Here clean code (which compiles well with my BCB 6.0 compiler)....
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
38
by: Radde | last post by:
HI all, Whats the difference b/w pass by ref and pass by pointer in C++ when ur passing objects as args.. Cheers..
16
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
3
by: Don Pasquale | last post by:
The following function intends to delete "numberoflines" lines from a text file, named "s" (string pointer) and pointed to by file pointer "fp", starting from line "line". Now, the function...
51
by: atv | last post by:
Hi, Just to check, if i set a pointer explicitly to NULL, i'm not allowed to dereference it? Why is that, it's not like it's pointing to any garbage right? Why else set it to NULL. I can remember...
7
by: Ray Dillinger | last post by:
Hi. I'm having a problem and I really want to understand it. Here's the situation: I have an array of pointers, and each pointer is the head of a linked list of structs. The structs are...
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
2
by: NotGuru | last post by:
I was writing some C extensions for Python and use PyTupleType_Check extensively. I found that all the PySomeType_Check macros directly delegate the job to PyObject_TypeCheck(op, &PyType_Type). The...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.