0

I'm trying to write function that search for char * element in array of char* and the function start check this element, if the element exist in the array I will have "found", if not it should be "inserted" and the element added to the array.

I wrote this code but I cannot know how to try it, the program always gives me exception, what can I do to check the element in my pointer array?

void checkFunction(char*myArray[], char *element,bool flag)
{
    for (int i = 0; i < strlen(*myArray) ; ++i)
    {
        if (myArray[i] == element)
        {
            flag = true;
        }
    }
    *myArray = element;
    flag = false;

    if (flag)
    {
        cout << "Found" << endl;
    }
    else
    {
        cout << "Inserted" << endl;
    }
}
2
  • 7
    You aren't ready to use poiners and arrays, which are advanced low-level language features for experts. Please use std::string and std::vector. Commented Dec 20, 2015 at 23:45
  • When you compare a char pointer with another char pointer, you're not comparing their values. You're comparing their positions on memory. You have to check them like that: strcmp(a, b) == 0; Commented Dec 20, 2015 at 23:46

2 Answers 2

2

C++ Way

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

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

    vector<string> myStrings { "One", "Two", "Three" };

    // std::find()  finds the first element that matches a value
    auto it = find(begin(myStrings), end(myStrings),  "Twooo");
    if (it != end(myStrings)) {
        cout << "We found this string; do something..." << endl;

    }


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

Comments

0

Few remarks regarding your function:

1.Why do you need the third parameter bool flag, instead of having it as local variable?

2.If you want to expand an array you should copy the old to a newly allocated and then add the new element, you can not just do: *myArray = element;

3.If you want to iterate through the array length/ size, instead of:

for (int i = 0; i < strlen(*myArray) ; ++i)

pass an additional parameter to your function, that indicates the number of elements in the array.

With std::string and std::vector you could do something like:

void check_insert (std::vector<std::string>& v, std::string& c) {

    for (auto i = 0; i < v.size(); ++i) {
        if (v[i] == c) {
            std::cout << "Found!\n";
            return;
        }
    }

    v.push_back(c);
    std::cout << "Inserted!\n";
}

3 Comments

Mostly good. But I think your for loop with arr_begin, arr_end is wrong. Each of those is a pointer to a char* anywhere in memory, not the location of the array elements themselves. itself. for(auto i = myArray; i != &myArray[n];++i)
@Keith a pointer to the first element of an array, when incremented n times, where n = array-size will 100% reach the last element, as "an array is a homogeneous sequence of objects allocated in contiguous memory."
Yes, exactly. However, your now deleted code was setting arr_begin to the value contained in first element, not the address of the first element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.