Strings and pointers

Discuss PocketC for Palm OS (including Desktop Edition)

Postby aharris on Tue May 15, 2001 4:03 pm

I'm working on the dreaded 'pointers' chapter.

I've run across some strange behavior, and I wanted to know if you've seen this too.

Here's my program:
//illustrates basic pointer concepts
//Andy Harris, 05/15/01

int number;
string message;

pointer pNumber;
pointer pMessage;

number = 1;
pNumber = &number;

alert("Number: " + number);
alert("pNumber: " + pNumber);
alert("*pNumber: " + *pNumber);

*pNumber = 2;
alert("Number: " + number);

message = "Hi there!";
pMessage = &message;
alert("*pMessage: " + *pMessage);
alert("*pMessage + 1: " + *(pMessage + 1));
} // end main

It works just fine, except when I print out the value at *pMessage, I get the ENTIRE string(!?!) I was just expecting to see the first character. When I look at the value of *pMessage + 1, it appears to be the next memory cell PAST the end of the string (which, of course is usually full of garbage).

Is this a feature (?) of pocketC? It surprises me that a string pointer would return back a full string.

Has anybody else noticed this phenomenon?

Posts: 10
Joined: Mon Jan 22, 2001 6:57 pm
Location: USA

Postby jstadolnik on Tue May 15, 2001 6:25 pm

PocketC implements strings as it's own seperate data type rather than as an array of chars (as you may be familiar with standard C).

As such, incrementing a pointer to a string, takes you to the next string rather than to the next character in the string.

This is actually quite useful in dealing with arrays of strings.

You can use the @[] operator to access individual characters in a string (though this operator does not work on pointers to strings).


BTW: You might consider adding a code optimization section/appendix to your book.
Posts: 1741
Joined: Wed Dec 06, 2000 3:34 am
Location: USA

Return to PocketC for Palm OS

Who is online

Users browsing this forum: No registered users and 4 guests