-3

This is an amateur question. I searched for other posts about this topic, found lots of results, but am yet to understand the concepts behind the solution.

This is a practice problem in my C++ book. It is not assigned homework. [Instructions here][1] .

WHAT I WOULD LIKE TO DO:

string input;
getline(cin, input); //Get the user's input.

int front = 0;
int rear;

rear = input.size();

WHAT THE PROBLEM WANTS ME TO DO

string input;
getline(cin, input); //Get the user's input.

int* front = 0;
int* rear;

rear = input.size();

Error: a value of type "size_t" cannot be assigned to an entity of type int*

This makes sense to me, as you cannot assign an 'address' of an int to the value of an int. So my questions are:

  • What is the correct way to go about this? Should I just forget about initializing front* or rear* to ints? Just avoid that all together? If so, what would be the syntax of that solution?
  • Why would this problem want me to use pointers like this? It's clear this is a horrible usage of pointers. Without pointers I could complete this problem in like 30 seconds. It's just really frustrating.
  • I don't really see an advantage to EVER using pointers aside from doing something like returning an array by using pointers.

Thanks guys. I know you like to help users that help themselves so I did some research about this first. I'm just really irritated with the concept of pointers right now vs. just using the actual variable itself.

Posts about this topic that I've previously read:

2
  • Tip to get started: If you want to reverse a string using pointers, probably it's better not to use an STL string, but an array of characters. Put your string in such an array, and then start swapping chars, the first swaps with the last etc.. So you have two pointers, one running from the start to the end of your string and the other backwards. Pointers are at the heart of C++. They enable you to build very efficient low-fat datastructures like trees an networks. Commented Jul 11, 2015 at 16:11
  • 1
    The book talk about C strings, front and read should be char*. Commented Jul 11, 2015 at 16:14

2 Answers 2

3

string.size() does not return a pointer - it returns size_t.

To revert a string try this instead:

string original = "someText";  // The original string
string reversed = original;    // This to make sure that the reversed string has same size as the original string

size_t x = original.size();    // Get the size of the original string

for (size_t i = 0; i < x; i++)        // Loop to copy from end of original to start of reversed
{
    reversed[i]=original[x-1-i];
}

If you really (for some strange reason) needs pointers try this:

string input;
getline(cin, input); //Get the user's input.

char* front = &input[0];
char* rear = &input[input.size()-1];

but I would not use pointers into a string. No need for it.

Sign up to request clarification or add additional context in comments.

3 Comments

How does .size() return a size_t when doing something like string word = "CAKE"; int x; int x = word.size(); cout << x; Would print 4 right? So why is size_t also considered an int?
@Hatefiend Because size_t is a typedef aliased to an integer type.
@Hatefiend : size_t can be casted to int. The difference is that int is not safe while size_t is.
1

I guest you may not quite understand the problem here. This problem want you to COPY a C string then REVERSE it by pointer operation. There is no classes in standard C. So, the C string is quite different from string class in C++. It is actually an array of char-type elements ended with character '\0'.

After understand this, you may start to understand the problem here. If you want to copy a C string, you can not just use str_a = str_b. You need constructor here. However, in pure C style, you should REQUIRE memory space for the string at first (you can use malloc here), then copy each element. For example, you want to create a function to make a copy of input string,

#include <string.h>

char *strcopy(char* str_in) {
    int len = strlen(str_in);
    char *str_out = (char*)malloc(len+1);

    char *in  = str_in;
    char *out = str_out;
    while(*in != '\0') { *out++ = *in++; }

    return str_out;
}

As you see, we actually use char* not int* here to operate string element. You should distinguish the pointer (such as in) and the element pointed by the pointer (such as *in) at first.

I'll show you a solution in pure C style for your problem, I hope this would help you to understand it. (You should be able to compile it without modification)

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

char* strreverse(char* in){
    // length of input string
    int len = strlen(in);
    // allocate memory for string operation
    char *out = (char*)malloc(len+1);
    // initialize <front> and <end>
    char *front = out, *end = out + len - 1;

    char buffer;

    // copy input string
    for(int i = 0; i <= len; i++){ out[i] = in[i]; }

    // reverse string
    for(; front < end; front++, end--) {
        buffer = *front;
        *front = *end;
        *end = buffer;
    }

    return out;
}

int main() {
    printf("REVERSE >> %s\n", strreverse("Hello, World!"));
    return 0;   
}

This is not you would do by C++ in actual programming, however, I guess the problem here is trying to let you understand mechanism of pointers. In this aspect, original C style would help a lot.

2 Comments

In your copy function, why not have like: codepad.org/FM5MLkif
That's a general array copy function that have to specify the length of array. However, the length of a C string can be evaluate by the end '\0'. The copy function here only work for C string not other array. Besides, here I use malloc for new memory space, which is a C style. Because C can not use variable to declare an array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.