-1

Not sure how to exactly explain this, sorry. I'm creating a function to find the first instance of a char in an array built by a given string. I have the function to create an array from the string and loop through the array, but not sure how to put it the array into the find function.

the tester is built like

stringName("Test test test");

stringName.find("e",0);  //where 0 is the starting position, so it would return 1. 
int SuperString::find(char c, int start) {
// put array grabber thing here
size = *(&data + 1) - data;
    for(int i = start; i < size ; i++){
        if(data[i] == c){
            return i;
            }
    }
   return -1;
   
    } 

This is what I have to make the string into an array.

SuperString::SuperString(std::string str) {
    size = str.size();
    data = new char[size];
    for (int i = 0; i < size; i++) {
        data[i] = str.at(i);
    }

}

This is probably something easy I'm missing, but any help is appreciated.

user4581301
  • 33,082
  • 7
  • 33
  • 54
sethro
  • 19
  • 2
  • Both kinds of string in C++ can be directly treated like arrays. – sweenish Oct 12 '21 at 21:13
  • Use `std::string` as the parameter type? Like you did in the constructor? – HolyBlackCat Oct 12 '21 at 21:13
  • @HolyBlackCat The parameters to `find()` are what to find and where to start; it's a class so it's already holding the string and doesn't need it passed. – sweenish Oct 12 '21 at 21:15
  • 1
    `"e"` is not a character. You likely meant `'e'`. – Drew Dormann Oct 12 '21 at 21:15
  • `"e"` is a `const char[2]`, `'e'` is a `char`. Have you tried `stringName.find('e', 0)`? – Nathan Pierson Oct 12 '21 at 21:15
  • @HolyBlackCat But I am seeing that the `data` member throws away a perfectly good `std::string` to improperly manage a heap-allocated C-string. They don't allow space for the null character. – sweenish Oct 12 '21 at 21:18
  • Also just make `data` itself a `std::string` instead of making it a `char*`, you're almost certainly going to have weird headaches caused by that. You can index into a string using `operator[]` the same way as with a character array, but it introduces fewer opportunities for error. – Nathan Pierson Oct 12 '21 at 21:18
  • The question and code are demonstrating some foundational gaps in OOP. [Here's a link](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to the good list. – sweenish Oct 12 '21 at 21:21

1 Answers1

1

You are passing a string literal, specifically a const char[2], where a single char is expected. Use 'e' instead of "e":

stringName.find('e', 0);

More importantly, size = *(&data + 1) - data; will only work when data is a (reference to a) fixed array (see How does *(&arr + 1) - arr give the length in elements of array arr?). It will not work when data is a pointer to an array, as it is in your case since you are allocating the array with new char[]. You will have to keep track of the array's size separately, which you appear to be doing, except that you are not actually using the size you obtained in the SuperString constructor. Just get rid of the line in find() that is trying to re-calculate size, use the value you already have:

int SuperString::find(char c, int start) {
    // size = *(&data + 1) - data; // <-- GET RID OF THIS
    for(int i = start; i < size; ++i){
        if (data[i] == c){
            return i;
        }
    }
    return -1;
} 

That being said, Your SuperString class can be greatly simplified if you just make its data member be a std::string instead of char*, eg:

#include <string>

class SuperString {
private:
    std::string data;
    ...
public:
    SuperString(const std::string &str);
    int find(char c, int start = 0);
    ...
};

SuperString::SuperString(const std::string &str) : data(str) {
}

int SuperString::find(char c, int start) {
    return (int) data.find(c, start);
} 
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770