-3

I am trying to write a function that takes a user submitted string and creates two pointers; one pointing to the first index of the string and one pointing to the last index of the string. I am getting an error when I try and set the pointers equal to the specific indexes firstPointer = userString[0]; secondPointer = userString[userString.length() - 1];. The error i'm receiving is appearing on the = operator and says "A value of type char can not be assigned to an entity of type char".

I've read several other posts on stackoverflow and I don't think they're addressing the same issue or it's plausible i'm not understanding what they're recommending. I'm fairly new to C++ and am new to pointers. You can ignore the for loop I was just setting that up when I realized my assignment of the pointers was throwing an error. Please find the function code below:

Function

void ReverseString(string &userString)
{

    char *firstPointer;
    char *secondPointer;

    firstPointer = new char;
    secondPointer = new char;

    firstPointer = userString[0];
    secondPointer = userString[userString.length() - 1];

    //Iterate over entire string
    for (int loopCounter = 0; loopCounter < userString.length()/2; loopCounter++)
    {

    }

}
StormsEdge
  • 854
  • 2
  • 10
  • 35
  • `firstPointer = new char;` for what purpose do you have these lines? – πάντα ῥεῖ Mar 07 '17 at 15:37
  • 1
    Initialization should be `secondPointer = &userString[userString.length() - 1];` – πάντα ῥεῖ Mar 07 '17 at 15:37
  • See [std::basic_string::operator\[\]](http://de.cppreference.com/w/cpp/string/basic_string/operator_at). – IInspectable Mar 07 '17 at 15:38
  • Oh! That fixed it, but why? I'm confused I thought you had to initialize the pointers first and then set them elsewhere by using new "type"? – StormsEdge Mar 07 '17 at 15:38
  • Also see [The Definitive C++ Book Guide and List](http://stackoverflow.com/q/388242/1889329). – IInspectable Mar 07 '17 at 15:39
  • 3
    @StormsEdge You don't need to allocate memory for a pointer if you're pointing it at an existing point in memory – Chris Turner Mar 07 '17 at 15:40
  • Also for those that down voted my question I feel the need to reiterate straight from the FAQ: No question is too trivial or too "newbie". Be tolerant of others who may not know everything you know. Yes, my question contains a syntax error, but I don't believe that constitutes it as a bad question. If my question is poor please let me know why so that I might fix it rather than just down voting it. This, in essence, prevents stack overflow from being exclusive and makes it inclusive IMO. – StormsEdge Mar 07 '17 at 16:06
  • The tooltip for the downvote button says: *"This question does not show any research effort"*. The question clearly fits that condition. – IInspectable Mar 07 '17 at 16:10
  • @IInspectable I disagree entirely. Please find me a question that relates directly to this; I also quoted that it's possible i'm not understanding something from other questions, which if i'm not mistaken, would explain for the error. Referencing a book and a link to Cpp documentation is hardly a reference to anything on the site. – StormsEdge Mar 07 '17 at 16:15
  • @IInspectable not to mention the documentation you tagged me in is in German, which frankly is useless to me as I do not speak German. – StormsEdge Mar 07 '17 at 16:16
  • [Click here for the English version of this page](http://en.cppreference.com/w/cpp/string/basic_string/operator_at). – IInspectable Mar 07 '17 at 16:21
  • @StormsEdge: "Research" doesn't just mean "Stack Overflow" - there are a number of other types of resource out there, and asking questions on SO is a *really* bad way to learn the language. A good book (preferably aimed at the sort of experience you already have), is much more likely to be helpful. – Martin Bonner supports Monica Mar 07 '17 at 17:07

2 Answers2

1

The error I'm receiving is appearing on the = operator and says
"A value of type char can not be assigned to an entity of type char".

I don't believe you. I am prepared to bet a significant sum that it actually says:
"A value of type char can not be assigned to an entity of type char *"

That trailling * is vitally important. firstPointer is not a char, it is a "pointer to char", or as we tend to say here "a char*".

userString[0] on the other hand, is a char (or more precisely, a reference to a char). It is a reference to the character in the string at index 0. If you want a pointer to that char, you need to take its address. So &userString[0]. So your code could be:

    firstPointer = &userString[0];
    secondPointer = &userString[userString.length() - 1];

Alternatively, getting the first and the last element is so common that there are specific functions for that, so you can write:

    firstPointer = &userString.front();
    secondPointer = &userString.back();

Finally, all of this depends on there actually being a first and last character - so you should really start with:

    if (userString.empty()) return;

to make sure there is.

-1

Try this:

void ReverseString(string &userString)
{

    char *firstPointer;
    char *secondPointer;

    firstPointer = &userString[0];
    secondPointer = &userString[userString.length() - 1];

    .....

}

Your problem is your userString isn't a pointer. A pointer is like the address of the actual object. So doing this: firstPointer = userString[0]; is like giving someone your house when they asked for your address. But you can get the "address" by using the & operator, like so firstPointer = &userString[0];.

You also don't need to use new in this situation. You would write something like firstPointer = new char; if you wanted your pointer to point to something that until that point didn't exist, effectively creating a new char and pointing to it. But, since the userString already exists, there is no need to create a new one.