0

I'm trying to use an array for function's input argument, when I call this function with a manual value, the function works good, but when I use an array with that value instead of the manual value, my function doesn't work correctly.

Example of my code:

Search("FileName","Word");
----------------------
Search("NotePad1","Hello"); >>> Work Correctly!
----------------------
But:
--------------------

char Word[25]={'H','e','l','l','o'};

Search("NotePad1",Word); >>>Doesn't Work! :-(
--------------------
2
  • 2
    try char Word[25]={'H','e','l','l','o','\0'}; Commented Jan 19, 2015 at 6:37
  • 2
    having a function declaration may be useful to see. Commented Jan 19, 2015 at 6:37

3 Answers 3

2

String literals such as "Hello" also include the NUL terminator byte at the end.

Add the terminator to your array as well:

char Word[25]={'H','e','l','l','o', 0};

Otherwise the C string read from the array will also contains any junk data at the end of the array and memory locations after it, up to the next zero byte in memory.

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

Comments

0

what about:

char* Word = "Hello";

depends on how you declared your function.

Comments

0

Assuming the function declaration to be as

boolean Search(String Filename ,String word);

try

char Word[]={'H','e','l','l','o','\0'}; // <- NULL terminated , a well formed string
Search("NotePad1" , Word);

It is because String in C/C++ and null terminated.

Conclusion :

If you are passing char array where argument expects a String make sure the char array is null terminated or pass string as "this".

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.