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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:17:44+00:00 2026-05-15T19:17:44+00:00

Code snippet to follow. I have a struct (example code has class, tried both,

  • 0

Code snippet to follow.

I have a struct (example code has class, tried both, same effect) that will store a number of char *. I made a constructor for the class that initializes most of them to = “”;
Upon attempting to modify that member of an instance of the class, strncpy et all report access denied. These functions even report access denied when i use them in the scope of internal struct/class member functions.

Its been my assumption that having a char * member of a struct/class was no big deal and nothing special (apart from being a pointer to a char, which required that it be initialized at some point before being used, and destroyed at certain times).

I appreciate it if someone could tell me where my assumptions have been wrong, and direct me to some literature that would clarify whats going on. I am currently compiling the code under vs2008 full edition.

I expect that someone who will run this code will get a runtime error about being denied access to a certain memory location.

I also expect that when I make a char * mystr that when I later say mystr = “”; that the memory is initialized for mystr to then be used.

I’d also like to think i’m not an idiot, but when I try to use the locals window to determine the precise in memory address of a certain variable, I can’t seem to get the ide to tell me where the memory is at. I ahve to get lucky in the memory window. oh well.

help! Thanks in advance. I hate dealing with these stupid char * cuz I always mess them up somehow, and my assumptions about how they work are flawwed. I want to change this so that I command them like the tools they are, not them frustrate me.

Thanks again for your consideration.
Josh

#include <stdio.h>
#include <string.h>



   class mystruct {
    public:
 int A;
 char* B;
 char* C;

 mystruct() {
  A = 0xFE;
  B = new char[32];
  B = "";
  C = "test";
 }

 void test(char *input) {
  strncpy(B,input, strlen(input));
 }
    };

    int main(char* argv[], int argc) {

 mystruct work;
 char wtf[32];

    // work.A = 0xbb;
 work.test("gotchabitch!");
    // sprintf(wtf, "address of work is %x" , &work);
    // strncpy(work.C,"gotchabitch!\0",strlen("gotchabitch!\0"));
  strncpy(work.B,"gotchabitch!",strlen("gotchabitch!"));
 printf("%d;%s;%s;", work.A, work.B, work.C); 

 return 0;
    } 

Response to all: Ok, I’ve learned that when you assign a string literal to a char * that you’re really saying char * const B = “something”. (isn’t that different from const char * B = something, where the latter is a a pointer whose address can not change, as opposed to the former which is a pointer to memory that can’t be changed?). Either way, this explains the error message that I got. Thank you all for that.

  • 1 1 Answer
  • 1 View
  • 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-15T19:17:45+00:00Added an answer on May 15, 2026 at 7:17 pm
    B = new char[32];
    B = "";
    

    You first assign the pointer returned by new to B, then you assign a pointer to the string literal "" to B, effectively overwriting the pointer returned by new.

    As Jonathan Leffler explains, string literals are read-only, so you encounter an error when you try to write to the string literal ("") pointed-to by B.

    If you want to store the empty string in B, you need to use a library function like strcpy() (or preferably, a safer function, like snprintf()). In this case, you could also just assign \0 to B[0], since that’s the same thing. In C and C++, you can’t assign arrays (or C strings, which are just arrays of characters) using =.

    Ideally, you should just use std::string instead of raw C strings; then you don’t have to worry about this sort of thing.

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

Sidebar

Related Questions

I have a snippet of code creating a new String as follow: private final
Suppose i have the code snippet as follows : ( clarification purpose/not well formed
The code snippet: C2DMessaging.java public class C2DMessaging { public static final String EXTRA_SENDER =
Which code snippet will give better performance? The below code segments were written in
Relevant Code Snippet: public class MyPreference extends Preference { public MyPreference(Context context, AttributeSet attrs)
The following is a code snippet of my application. Bear in mind that I
I have a simple doubly linked list example that I'm working on, but for
I have a snippet of code, simply trying to execute a script on a
I don't know if the follow code snippet intend to work in this way,
I have created a class (ConflictingBooking) that contains fields for various information in regards

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.