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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:31:21+00:00 2026-05-17T06:31:21+00:00

I have a simple class Name: class Name { private: char nameStr[30]; public: Name(const

  • 0

I have a simple class Name:

   class Name
{ private:
    char nameStr[30];
  public:
    Name(const char * = "");
    char * getName() const;
    void print() const;
};

I am having two problems, which are just a bit difficult for me wrap my head around. The constructor is supposed to take an argument of const char * and copy that into the nameStr array. However, this is what I have tried and is not working:

Name::Name(const char * setName)
{
    &nameStr = setName;
}

It gives me the error “invalid lvalue in assignment”. If I remove the reference in front of nameStr, it still errors, saying “incompatible types in assignment of ‘const char*’ to ‘char[30]'”. Not quite sure what I’m missing.

The other problem is the accessor method:

char * Name::getName() const
{
    return  &nameStr[0];
}

I have the same problem, it errors “invalid conversion from ‘const char*’ to char*'” and I can’t for the life of me figure out how to make it return a pointer to a const char… I’ve been searching to avail. Found plenty of info on const, but can’t quite figure how to apply it to my situation. Any help would be greatly appreciated!

  • 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-17T06:31:23+00:00Added an answer on May 17, 2026 at 6:31 am

    I would recommend you use an std::string, but first you need to know how pointers work.

    Let’s take your first problem: &nameStr = setName;

    Here, nameStr is an array of chars, for storing your name. &nameStr refers to the address of this array. You cannot set the address of a variable of any sort, it is illegal. For example, &a = &b (where a and b are ints) is just as illegal. Next you tried, nameStr = setName; and it said incompatible conversion from const char* to char[30]. What you are trying to do is assign one pointer to another. This doesn’t actually copy the data. To copy the data, you need to do this manually, e.g.:

    for (int n=0;(n>0?setName[n-1]!=0:true);n++) { nameStr[n] = setName[n]; }
    // The above won't work (or might crash) if the ternary operator evaluates both sides. I don't think it does, but correct me if I'm wrong.
    

    Note, the comparion with setName[n-1] instead of setName[n] is so the NULL character gets copied.

    For your next problem, cannot convert const char* to char*. Taking the address of a variable will always yeald a constant pointer, because you are not allowed to change the address. Also note that &array[0] is essentially the same as array because you a dereferencing, then re-referencing it, thus making the process unnecessary. You need to state your getName() function as returning const char* and in it, return nameStr.

    A word on arrays: Arrays are like other datatypes, except that they contain multiple of a datatype, not one. When referring to an array, its type is of a pointer to its member type. For example, an array of ints would be treated like an int*. The pointer points to it’s first element. This means that using [] on an array will dereference the pointer to any of its elements.

    Good luck with C++!

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

Sidebar

Related Questions

I have a simple class Role: @Entity @Table (name = ROLE) public class Role
I have a simple person class: package simpleApp.entities; public class Person { private String
I have the simple class using auto-implemented properies: Public Class foo { public foo()
For example I have a simple class like public class Person{ public int Age
I have a really simple class with two methods; One that will be called
Let's say I have a simple class Cat in C#, with a Name property
I have an entity class: Cabbage.java package womble; public class Cabbage { private int
If I have a simple class setup like this: class MyClass { private string
I have a simple class that looks like this: public class Item { //
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)

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.