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 285339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:32:14+00:00 2026-05-12T05:32:14+00:00

I am having trouble assigning the value of an NSString to a char *

  • 0

I am having trouble assigning the value of an NSString to a char * within a structure of a singleton class. The following is a simplification of my problem. My code utilizes more fields in the structure and passes more NSStrings.

Say I have a singleton class “SingletonClass” with a structure (I’ve done my homework with the Apple Documentation and implemented it correctly):

struct sampleStruct {
    char *string1;
    //other fields
} struct1;

with this method that sets the value of struct1.string1 to the NSString that was passed:

- (void)initStructWithString:(NSString *)inputString {
     //warning: assignment discards qualifiers from pointer target type
     struct1.string1 = [inputString cStringUsingEncoding:NSUTF8StringEncoding];
}

and this method that uses struct1.string1:

- (void)useCharArray {
     //I would obviously be doing something more complex in reality
     printf("This is the char array: %s", struct1.string1);
     //doesn't print the value I assigned to it, but garbage
}

When I call initStructWithString:(NSString *)inputString from one class and try to call useCharArray from another class struct1.string1 is a bunch of garbage. During debugging I’ve confirmed that each class is getting the same instance of this SingletonClass.

I’m still learning Objective-C as well as C along the way, so I’m aware it could be a problem with my memory management. I know I [NSString cStringUsingEncoding] should be assigned to a const char * and not a char *, but I don’t know how else to go about it since the field in the structure is a char *. I’ve tried other approaches within initSructWithString such as assigning a new const char * the result of [NSString cStringUsingEncoding] and then copying that with strlcpy to struct1.string1. That approach had the same result.

What is the correct technique for storing the value of an NSString in a char * within a struct and how can I ensure that the fields within this struct retain their value as the SingletonClass is used by different classes?

  • 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-12T05:32:15+00:00Added an answer on May 12, 2026 at 5:32 am

    One thing worth noting is that the correct method name would be cStringUsingEncoding:; and that this string disappears roughly at the same time inputString does, so the resulting string will have to be copied.

    It might also be worth exploring the use of UTF8String instead (a convenience method that does the same).

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

Sidebar

Related Questions

I'm having trouble assigning an instance of a class to the following UITableViewController subclass:
I am having trouble over simply assigning a value to a UI Button: //Assume
I am having trouble assigning a NSString to the text of a NSScrollView. I
Well new to the java stuff and having trouble with assigning a class attribute
I'm having trouble assigning a value to my 2D array in Java. The last
I'm using rails 3.2.3 and am having trouble assigning a value in my HABTM
Having trouble with each function... Will try to explain by example... In my code,
Having trouble accessing javascript code in a mixed html/js ajax response. jQuery ajax doc
Having trouble understanding. With the following css : .bloc .field:nth-last-child(2){ ...some values... } and
I am having some trouble getting this simple code to work: #pragma once #include

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.