4

I'm experiencing an unexpected interaction with char array initialization.

When initializing a char[] with a size of strlen( char parameter[] ), the newly initialized char[] is too big. I am not sure what is causing this.

int main(void)
{
    char myString[] = { " " };
    foo( myString );
}    


int foo( char str[] )
{
    char testString[ strlen( str ) ];
    printf( "Length of testString: %lu\n", strlen( testString ) );
    return 0;
}

When I run foo, the output is

Length of testString: 6 

when I am expecting it to be 1.


Even stranger is when I add a print statement to foo before the initialization of testString, the output seems to magically fix itself:

int foo( char str[] )
{
    printf( "Length of str: %lu\n", strlen( str ) );
    char testString[ strlen( str ) ];
    printf( "Length of testString: %lu\n", strlen( testString ) );
    return 0;
}

foo now prints

Length of str: 1
Length of testString: 1

I have a feeling that it has something to do with the way char[] are passed into functions, or perhaps an unexpected behavior of strlen, but I really have no idea.

Any help is appreciated.

2
  • 1
    Um... You are not initializing your testString array at all. You specified the size, but the array itself is left uninitialized: it contains garbage. Trying to apply strlen to an uninitialized array results in undefined behavior. That's what you are observing. And yes, undefined behavior can and will behave in unpredictable way and might even appear to "magically fix itself". Commented Feb 3, 2017 at 0:59
  • stackoverflow.com/questions/16381307/… Commented Feb 3, 2017 at 0:59

2 Answers 2

5

The array was not initialized

char testString[ strlen( str ) ];

Thus applying the function strlen to it results in undefined behavior.

printf( "Length of testString: %lu\n", strlen( testString ) );

Take into account that you may not initialize a variable length array like testString along with its declaration. You could write for example

char testString[ strlen( str ) ];
testString[0] = '\0';

It seems that what you mean is the sizeof operator. If so you can write

printf( "Length of testString: %zu\n", sizeof( testString ) );

Here is a demonstrative program

#include <stdio.h>
#include <string.h>

void foo( const char str[] )
{
    char testString[ strlen( str ) ];
    printf( "Length of testString: %zu\n", sizeof( testString ) );
}

int main(void) 
{
    char myString[] = { " " };
    foo( myString );

    return 0;
}

Its output is

Length of testString: 1
Sign up to request clarification or add additional context in comments.

1 Comment

This did the trick, thanks! I obviously need to brush up on my understanding of array behavior.
0

remember two things:

  1. strlen() yields the offset to the NUL char in the string and
  2. offsets into arrays start with 0, not 1

and now the code:

#include <string.h> // strlen(), strcpy()
#include <stdio.h>  // printf()

int main(void)
{
    char myString[] = { " " };  <<-- actual length 2 (a space + a NUL )
    foo( myString );
}    


int foo( char str[] )
{
    char testString[ strlen( str ) ]; <<-- strlen() yields 1, and testString not initialized
    printf( "Length of testString: %lu\n", strlen( testString ) ); <<-- strlen searchs until a NUL byte found which could be anywhere
    return 0;
}

Suggested corrections:

int foo( char str[] )
{
    char testString[ strlen( str )+1 ];
    strcpy( testString, str );
    printf( "Length of testString: %lu\n", strlen( testString ) );
    return 0;
}

Note: the output of the above/corrected code is 1, but the array testString[] is actually 2 bytes

another way to do it is:

int foo( char str[] )
{
    char testString[ strlen( str ) ] = {'\0'};
    printf( "Length of testString: %lu\n", strlen( testString ) );
    return 0;
}

then the output would be 0 and the actual length of testString[] is 1

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.