0

I'm attempting to search for a string in a character array whereby the string to be identified is UCAST and whereby the character array is as follows:

analysis_buffer[0] = \n;
analysis_buffer[1] = U;
analysis_buffer[2] = C;
analysis_buffer[3] = A;
analysis_buffer[4] = S;
analysis_buffer[5] = T;
analysis_buffer[6] = \r;

The code I executed to attempt the search procedure is as follows:

            constant char str[] = "UCAST";
        char* pch = strstr(analysis_buffer,str);

        if (pch!=NULL) {
            printf("found at %d\n", pch - analysis_buffer + 1);

            pch = strstr(pch+1, analysis_buffer);
        } else {
            printf("pch :%s\n", pch);
        }

NOTE: analysis_buffer is the array I described above.

The problem I'm encountering is that the strstr bit amounts to NULL while it shouldn't. Any ideas?

I've had a look at the following links on stackoverflow for help:

Does char array contain string?

How to search a string in a char array in C?

THANKS

4
  • Welcome to SO. Don't change the original question in response to posted answers. It is confusing for people coming to the question and makes posted answers superfluous. Commented Feb 24, 2014 at 16:07
  • Apologies .. wont do that again Commented Feb 24, 2014 at 16:11
  • Does it compile? What do actually assign to analysis_buffer? Commented Feb 24, 2014 at 16:13
  • Hi all, it compiles when set y analysis_buffer as a char buffer as indicated by hmjd. Apologies but I forgot to mention that analysis_buffer is an int8 array not a char array hence it is still NOT working. Please see my post here stackoverflow.com/questions/22011923/… whereby I'm asking how to copy a elements from an int8 array into a char array Commented Feb 25, 2014 at 10:57

1 Answer 1

3

The code is incorrect as the array is one element too short for the initializer:

char str[5] = "UCAST";

due to implicit null character added to all string literals. From section 6.4.5 String literals point 5 of the C99 standard:

In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literal. ...

Possible fixes:

  • allow the deduction of the required array size:

    char str[] = "UCAST";
    
  • use a const char* as it is unrequired for str to be modified:

    const char* str = "UCAST";
    

Note that the string being searched (analysis_buffer in this case) must be null terminated also. From the posted code it is not apparent that this is happening.

Additionally, the final printf() statement is incorrect as the format specifier is %c but the argument (pch) type is char*. Normally, either use %s and use pch or use %c and *pch BUT pch is NULL in this branch of the if so it cannot used in the printf().

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

7 Comments

Ah I see .. so how do I store the string I would like to search for .. may be like this .. char str[] = "UCAST";
@user34395, refresh your page as i have that in this answer.
just saw it. lemme try it out.
Also note that char str[] = "..."; allocates the string on the stack (so it uses 6 bytes of stack space in this case), which may be a problem depending on what you want to do (probably not in this case). In order to not use stack space for the string itself, use char* str = "..." -- but note that the string cannot be modified in this case.
@hmjd: I've applied the constant char as seen in the edited code in my orginal post but it doesn't work. analysis_buffer is the char array to be searched and it is null terminated. I've also changed the %c to %s as I'll stick to using pch.
|

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.