2

I often want to do something like this:

unsigned char urlValid[66] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

...or:

unsigned char listOfChar[4] = "abcd";

...that is, initialize a character array from a string literal and ignoring the null terminator from that literal. It is very convenient, plus I can do things like sizeof urlValid and get the right answer.

But unfortunately it gives the error initializer-string for array of chars is too long.

Is there a way to either:

  • Turn off errors and warnings for this specific occurrence (ie, if there's no room for null terminator when initialising a char array)
  • Do it better, maintaining convenience and readability?
6
  • 2
    What about std::string instead? Commented Jan 17, 2013 at 20:04
  • @David I code primarily in C, any alternatives for that? Commented Jan 17, 2013 at 20:05
  • 7
    If this is C, then remove the C++ tag. Commented Jan 17, 2013 at 20:05
  • 2
    Wait, you want to use C in a C++ project? Your teammates will hate you... Commented Jan 17, 2013 at 20:12
  • 1
    @Alec you are aware that C and C++ are different languages which “by accident” share some of the syntax and one character in the name? Commented Jan 17, 2013 at 20:19

4 Answers 4

8

You tagged your question as both C and C++. In reality in C language you would not receive this error. Instead, the terminating zero simply would not be included into the array. I.e. in C it works exactly as you want it to work.

In C++ you will indeed get the error. In C++ you'd probably have to accommodate the terminating zero and remember to subtract 1 from the result of sizeof.

Note also, that as @Daniel Fischer suggested in the comments, you can "decouple" definition from initialization

char urlValid[66]; 
memcpy(urlValid, "ab...", sizeof urlValid);

thus effectively simulating the behavior of C language in C++.

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

4 Comments

"terminating zero simply would not be included". That is interesting. Could you give me the reference?
@Nawaz 6.7.9 (14) "An array of character type may be initialized by a character string literal or UTF−8 string literal, optionally enclosed in braces. Successive bytes of the string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array."
@Nawaz: It is in 6.7.8/14 in C99. "An array of character type may be initialized by a character string literal, optionally enclosed in braces. Successive characters of the character string literal (including the terminating null character if there is room or if the array is of unknown size) initialize the elements of the array." Note the "if there is room" part. This has always been one of the differences between C and C++.
In C++, he could also unsigned char urlValid[66]; memcpy(urlValid, "ab...", sizeof urlValid);. Whether that's more convenient than the 0-terminator is up to the OP to decide.
1

Initialise with an actual array of chars?

char urlValid[] = {'a','b','c','d','e','f',...};

Comments

0

Well, in C++ you should always use std::string. It's convenient and not prone to memory leaks etc.

You can, however, initialize an array without specifying the size:

char urlValid[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";

This works since the compiler can deduce the correct size from the string literal. Another advantage is that you don't have to change the size if the literal changes.

Edit:You should not use unsigned char for strings.

8 Comments

That adds a null terminator, which makes sizeof urlValid return 67
@Alec See, why are you so worried about that terminating NUL? That's how strings work in C. You should get used to it.
Maybe because he wants a char array and not a string, @H2CO3.
Why downvote? The string will be hard to work with using standard functions like strcpy if it doesn't have a terminating zero.
I suppose that this is not supposed to be worked with as a string. It looks more like a lookup array or something, although I'd implement such a lookup differently
|
-2

there are two simple solutions.

  1. You can either add an extra element into the array like this:

    unsigned char urlValid[67] = 
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
    
  2. or leave out the size of the array all together:

    unsigned char urlValid[] =
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~";
    

1 Comment

The OP wants the exact opposite. He wants to ignore the trailing \0.

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.