0

I was trying to make a string search from an array..

look at this....

int   TotalBadStrings = 3;  
 // START INFINITE SCANNING!     
   while(true)
   {

   for (int BadStringsCount = 0; BadStringsCount < TotalBadStrings; BadStringsCount++)
       {

        char* StringsToSearch[] = {"badstring1", "badstring2"};

  char *lpData = (CHAR*)GlobalAlloc(GMEM_FIXED, MAX_READ),
       lpOrig[]     = StringsToSearch[BadStringsCount];


// HERE I MAKE A SCAN IN MEMORY USING lpOrig

   }

When i try to compile i get this

[Error] initializer fails to determine size of 'lpOrig'

214 54 [Error] array must be initialized with a brace-enclosed initializer

The problem is when I try to get one of the strings from my array StringsToSearch

I am using DEV C++ 5.6.1 with GCC 4.8.1 32 bits

Any idea? thank you in advance!

3
  • Arrays cannot be initialized with pointers. And don't ever use a non-const char * to point to a string literal. You're better off just using std::string anyway. Commented Feb 18, 2014 at 20:05
  • I don't see lpOrig declared anywhere, just being used. Commented Feb 18, 2014 at 20:11
  • Could you give me an example of putting this strings into a std::string and call it in my for ? Commented Feb 18, 2014 at 20:22

2 Answers 2

1

I think you meant to write:

const char* lpOrig = StringsToSearch[i];

But it's still quite unclear what you are trying to do. But you should use vector and string instead. This is how you initialize it, with let's say the array you already got there.

  #include <string>
  #include <vector>

  ...

  std::vector<std::string> strings;     
  strings.assign(&StringsToSearch[0], &StringsToSearch[sizeof(StringsToSearch)/sizeof(StringsToSearch[0])]);

A fully working example:

#include <iostream>
#include <string>
#include <vector>

int main(int argc, char* argv[])
{
    const char* StringsToSearch[] = {"asd1", "asd2", "asd3"};

    std::vector<std::string> strings;       
    strings.assign(&StringsToSearch[0], &StringsToSearch[sizeof(StringsToSearch)/sizeof(StringsToSearch[0])]);
    std::vector<std::string>::const_iterator begin = strings.begin();
    while (begin != strings.end())
    {
        std::string s = *begin;

        std::cout << "Comparing: " << s << std::endl;
        //do you comparison here

        ++begin;
    }

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

1 Comment

I am trying to set lpOrig EQUAL to EACH STRING i have I think i cant adapt this code :x
0

I suggest adding char * at the beginning of lpOrig to define it if this has not yet been done.

char *lpOrig[]     = StringsToSearch[BadStringsCount];

I also see that this could cause a segmentation fault when you run it if BadStringsCount is too large. If I understand your code correctly, the only valid values for BadStringsCount should be 0 or 1 because you assign 2 bad strings to check for, and arrays are 0 indexed in C++ (for example, the first element of the array is at position 0). You may want to make the following change to the first line to fix this:

int   TotalBadStrings = 2;  

Comments

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.