1

Apologies if my following question sounds trivial but I'm struggling with the fundamental concept of "converting an a char array to a string" for use in the method strstr().

Essentially I have an array called EUI_only[] which is dynamically populated at a certain stage of my program i.e.

EUI_only[0] = A;
EUI_only[1] = B;
EUI_only[2] = C;
EUI_only[3] = D;
EUI_only[4] = E;

I declared EUI_only as follows at the top of my program:

char EUI_only[];

I can confirm that EUI_only[] is successfully populated as justbefore I use it in method strstr() I print it out.

Now I have the second array called receive_key_press_temporary_analysis_buffer which I intend on utilising in my strstr() i.e. the array which contains characters similar to those in EUI_only.

This array is also dynamically populated in a certain point of my program and I can confirm that the contents of the array are intact just before I use it in strstr() as I've printed this array.

Content of this array called receive_key_press_temporary_analysis_buffer can be seen below:

receive_key_press_temporary_analysis_buffer[0] = 1;
receive_key_press_temporary_analysis_buffer[1] = 2;
receive_key_press_temporary_analysis_buffer[2] = C;
receive_key_press_temporary_analysis_buffer[3] = A;
receive_key_press_temporary_analysis_buffer[4] = B;
receive_key_press_temporary_analysis_buffer[5] = C;
receive_key_press_temporary_analysis_buffer[6] = D;
receive_key_press_temporary_analysis_buffer[7] = E;
receive_key_press_temporary_analysis_buffer[8] = 3;
receive_key_press_temporary_analysis_buffer[9] = 4;

Below essentially is how I use both arrays in strstr():

char*  is_eui_only_content_in_receive_key_press_analysis = strstr(receive_key_press_temporary_analysis_buffer, EUI_only);

printf("\n\rResult of is_eui_only_content_in_receive_key_press_analysis: %s\n\r", is_eui_only_content_in_receive_key_press_analysis);

Basically when I print the outcome of strstr() it gives me:

Result of is_eui_only_content_in_receive_key_press_analysis: CDE34

On the other hand if I reinitialise my EUI_only follows:

EUI_only[] = "ABCDE";

Essentially I get the expected result from the strstr() i.e.

Result of is_eui_only_content_in_receive_key_press_analysis: ABCDE34

Why could it be the strstr() doesn't give me my expected result when I utilise my dynamically populated EUI_only[] array?

Thanks, Alex.

3
  • Are both of your archives have terminating zero on the end of data? Commented Apr 10, 2014 at 10:58
  • Why are you describing your code rather than showing it? Concealing code from us hardly helps. Commented Apr 10, 2014 at 10:58
  • Make sure when you assign dynamically the arrays should be terminated by null character '\0'. When you initialised with "ABCDE", by default null character is padded Commented Apr 10, 2014 at 11:00

2 Answers 2

5

If your only definition is char EUI_only[]; then it actually has a size of 1. You cause a buffer overflow by writing 5 characters to it. In C, arrays don't automatically resize; they have a fixed size for their lifetime.

Successfully printing its contents doesn't prove anything, as it could perhaps happen that you write into memory you don't own, but nothing disturbs that memory in between the write and the read.

Secondly, the str functions expect a string. The definition of a string is a series of characters followed by a null character. In your example code you do not have the null character, so even if you did not have the allocation problem, you don't actually have a string; therefore the function misbehaves.

When you write char EUI_only[] = "ABCDE"; , it is doing char EUI_only[6] = { 'A', 'B', 'C', 'D', 'E', 0 }; which is a string, so everything works as expected.

Update: The reason the array size would be 1 is covered by C99 6.9.2, "tentative definitions". After the line char EUI_only[];, EUI_only has incomplete type. For an incomplete array type, you can dereference it, but you can't do sizeof on it. You may complete the type at any point later on in the source file; and if you don't, then it behaves as if the last line of the source file was char EUI_only[] = { 0 };. This is a rarely-used feature of C; usually you would declare a complete type by either specifying the size, or providing an initializer.

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

6 Comments

char EUI_only[]; defines an array of size 1? Isn't it an incomplete type in the translation unit it's contained in?
Yes, but if you reach the end of file without completing it, it is as if you wrote char EUI_only[] = { 0 }; at the end of the file. See C99 6.9.2 ("tentative definition").
Legal use of array tentative definition: char x[]; int main() { x[5] = 1; } char x[6];
Thanks much :) I looked it up in the standard and it's clear to me now though I'd say if someone uses this feature in their code, it needs to be disapproved of.
Also, please add your comment to the answer. It will be clearer.
|
0

C strings are terminated by null '\0' character

EUI_only[5] = '\0' ;

and

receive_key_press_temporary_analysis_buffer[10] = '\0' ;

Make sure you have extra space for null character at declaration.

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.