Professional Web Applications Themes

NSString -> c string warning - Mac Programming

In <clund-E17CA6.11533517082003amstwist00.chello.com> C Lund wrote: > The following: > > char *textString; > textString = ["abcdefghijklmnopqrstuvwxyz" cString]; > > gives me an "assignment discards qualifiers from pointer target type" > warning. Because you didn't say const. m. -- matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url] REALbasic: The Definitive Guide! 2nd edition! [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url] Subscribe to TidBITS. It's free and smart....

  1. #1

    Default Re: NSString -> c string warning

    In <clund-E17CA6.11533517082003amstwist00.chello.com> C Lund wrote:
    > The following:
    >
    > char *textString;
    > textString = ["abcdefghijklmnopqrstuvwxyz" cString];
    >
    > gives me an "assignment discards qualifiers from pointer target type"
    > warning.
    Because you didn't say const. m.


    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  2. #2

    Default Re: NSString -> c string warning

    In article <clund-E17CA6.11533517082003amstwist00.chello.com>,
    C Lund <clundNOSPAMnotam02.no> wrote:
    > The following:
    >
    > char *textString;
    > textString = ["abcdefghijklmnopqrstuvwxyz" cString];
    >
    > gives me an "assignment discards qualifiers from pointer target type"
    > warning.
    >
    > It works the way it's supposed to, but I *am* getting a warning. Is this
    > about a loss of information I don't need or a symptom of something more
    > serious? Should I just ignore it?
    >
    > "lossyCString" gives the same result, btw.
    As another poster said, you need your variable declared const, because
    these methods return const strings. In other words, you're not allowed
    to change the string they return.

    These methods are fairly strange. They return a string pointer that is
    effectively autoreleased, even though you can't autorelease things that
    aren't objects. I think they make an NSData object that contains the
    string, and give you the pointer to NSDatat's bytes. The point of this
    is that your textString will get pulled out from underneath you whenever
    the autorelease pool is popped. This may be ok for your application, but
    if it's not, copy the returned string so you have control over how long
    it stays.

    Also, most of the time, [string UTF8String] is what you'll want to use.
    This will give you ASCII codes for ASCII characters, and use a non-lossy
    unicode encoding for non-ASCII characters that won't interfere with any
    C-string handling routines. This lets you correctly store non-english
    scripts in C strings, should you need to do so. And you never know what
    the user might type into a text box.

    If you know for sure that you don't have any non-ASCII, then UTF8String
    and cString will be the same except possibly for performance. So why not
    just use UTF8String everywhere, and you won't accidentally use cString
    for something the user has control over.

    Ok, this post is probably about 50% too long, so I'll stop now. :P
    Michael Ash Guest

  3. #3

    Default Re: NSString -> c string warning

    In article <mail-52536A.10050317082003localhost>,
    Michael Ash <mailmikeash.com> wrote:
    > > char *textString;
    > > textString = ["abcdefghijklmnopqrstuvwxyz" cString];
    > As another poster said, you need your variable declared const, because
    > these methods return const strings. In other words, you're not allowed
    > to change the string they return.
    textString was to be sent to another class. Recieving it as a "const
    char *" did the trick.
    > Also, most of the time, [string UTF8String] is what you'll want to use.
    > This will give you ASCII codes for ASCII characters, and use a non-lossy
    > unicode encoding for non-ASCII characters that won't interfere with any
    > C-string handling routines. This lets you correctly store non-english
    > scripts in C strings, should you need to do so. And you never know what
    > the user might type into a text box.
    Makes sense. It makes no difference for my current use, but I might as
    well make a habit of it.

    --
    C Lund, [url]www.notam02.no/~clund[/url]
    C Lund Guest

  4. #4

    Default Re: NSString -> c string warning

    In <clund-136B47.18324217082003amstwist00.chello.com> C Lund wrote:
    > In article <mail-52536A.10050317082003localhost>,
    > Michael Ash <mailmikeash.com> wrote:
    >> Also, most of the time, [string UTF8String] is what you'll want to
    >> use. This will give you ASCII codes for ASCII characters, and use a
    >> non-lossy unicode encoding for non-ASCII characters that won't
    >> interfere with any C-string handling routines. This lets you
    >> correctly store non-english scripts in C strings, should you need to
    >> do so. And you never know what the user might type into a text box.
    >
    > Makes sense. It makes no difference for my current use
    Not so. Listen more attentively to what Michael is trying to tell you.
    cString has been deprecated for a year now, with a warning that it may
    not even be supported in future versions of the system. Never use it. It
    has a dangerous feature: it relies upon the "default encoding", and you
    have no way of controlling this. So cString can have different results
    on different machines. m.

    --
    matt neuburg, phd = [email]matttidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

Similar Threads

  1. new warning in 1.8 from String#split
    By Phil Tomson in forum Ruby
    Replies: 0
    Last Post: October 11th, 06:14 PM
  2. NSString in NSDateFormat'ed cell
    By matt in forum Mac Programming
    Replies: 0
    Last Post: September 10th, 04:50 AM
  3. Cocoa: put returns in NSString to keep it to a certain width
    By Simon Slavin in forum Mac Programming
    Replies: 2
    Last Post: September 8th, 10:33 PM
  4. regexp warning in String.split?
    By Chris Morris in forum Ruby
    Replies: 3
    Last Post: August 15th, 01:15 PM
  5. Escape sequence for unicode characters in NSString
    By Eric Raas in forum Mac Programming
    Replies: 2
    Last Post: July 9th, 02:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139