Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:34:13+00:00 2026-05-15T15:34:13+00:00

When converting a NSString to a char* using UTF8String, how to retain it? According

  • 0

When converting a NSString to a char* using UTF8String, how to retain it?

According to the following link, when you use UTF8String the returned char* is pretty much autoreleased, so it won’t last beyond the current function: http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSString_Class/Reference/NSString.html#jumpTo_128

It says i have to copy it or something to keep it around. How can i do this?

The reason i ask is that if i do [myCharPointer retain] it doesn’t retain it because it’s not an obj-c object, it’s a c pointer.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:34:14+00:00Added an answer on May 15, 2026 at 3:34 pm

    You can use strdup()

    const char* utf8Str = [@"an example string" UTF8String];
    if (utf8Str != NULL) 
    {
        stringIWantToKeep = strdup(utf8Str);
    }
    

    When you are done with stringIWantToKeep, you free it as though it was originally malloc’d.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following function for converting an NSString to an image. -(UIImage *)imageFromText:(NSString
When converting NSString to constants, I usually use [@... UTF8String]; I was just looking
I am converting date from NSString to NSDate using following code NSString *dateString =
Am converting a webpage as a pdf file. I did the following, NSString *string=[NSString
I'm using following code to get NSDate, convert it to NSString and then back
When I was converting a char * to NSString in my App, I got
I've tried converting the same NSDictionary object into NSData and then NSString using NSJSONSerialization
I was converting NSString to NSData in order to parse by JSON, but I
I'm having a strange issue converting a string to a date: NSString *goodDateString =
After converting project to ARC, The converting operation removed all the - assign, retain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.