Re: trouble getting c-chars from NSString
Re: trouble getting c-chars from NSString
- Subject: Re: trouble getting c-chars from NSString
- From: Justin Spahr-Summers <email@hidden>
- Date: Wed, 30 Mar 2005 21:38:14 -0600
There are a great deal of problems with this code.
> while (charPtr != '\0') {
First of all, comparing charPtr to a NUL character isn't going to do
what you expect. I think you mean charPtr[0].
> if (charPtr >= '\0' && charPtr <= '\9')
Here, you compare charPtr (once again, a pointer, not a character) to
a NUL character, and the character represented by ASCII value 9. You
should do (charPtr[0] >= '0' && charPtr[0] <= '9') instead.
> newWord[numCharsInNewWord++];
This is a statement with no effect. You are only incrementing the
variable 'numCharsInNewWord'. I think you mean to assign the first
character from charPtr to a new index in newWord. Try
newWord[numCharsInNewWord++] = charPtr[0];
Also, I would caution against dynamic arrays with an unknown size. A
better idea would be to use a pointer to some malloc()'d memory that
was allocated with the length of the C string, as returned by
[original cStringLength]. In general, you should be using the UTF8
string instead so that none of the data is lost (other than what you
want to be gone) when the numbers are removed.
I suggest you read more up on C.
On Wed, 30 Mar 2005 16:48:26 -1000, Daniel Child <email@hidden> wrote:
> Hi All,
>
> Sorry, I am a newbie and I only did a tiny bit of C, so this question
> is pretty basic. I am trying to convert an NSString to a C-String so
> that I can remove certain characters. Somehow I don't seem to be able
> to get the cString returned from the [NSString cString] method to be
> assigned to a character array.
>
> Here's what I have at this point.
>
> NSString *original = @<some string of characters that contains
> digits> e.g. asd5sd
> char *origChars = [original cString]; // my c-string
> char *charPtr;
> char charArray[];
> char newWord[]; // a word without numbers
> int numCharsInNewWord
>
> If I try to assign origChars to either *charPtr or charArray, I get an
> error. What I'm hoping to do is to get a word that is devoid of numbers.
>
> numCharsInNewWord = 0;
> while (charPtr != '\0') {
> if (charPtr >= '\0' && charPtr <= '\9')
> ; // don't copy digits
> else
> newWord[numCharsInNewWord++];
> charPtr++;
> }
>
> Thanks in advance!
>
> Daniel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden