14

I am writing a program and I need to initialize a message buffer which will hold text. I am able to make it work, however I am writing below various ways used to initialize the strings in C and I want to understand the difference. Also, which is the most appropriate method for initializing a wchar_t/char string?

Method I:

wchar_t message[100];

based on my understanding, this will allocate a memory space of 200 bytes (I think size of wchar_t is 2 bytes on Windows OS). This memory allocation is static and it will be allocated inside the .data section of the executable at the time of compiling.

message is also a memory address itself that points to the first character of the string.

This method of initializing a string works good for me.

Method II:

wchar_t *message;
message=(wchar_t *) malloc(sizeof(wchar_t) * 100);

This method will first initialize the variable message as a pointer to wchar_t. It is an array of wide characters.

next, it will dynamically allocate memory for this string. I think I have written the syntax for it correctly.

When I use this method in my program, it does not read the text after the space in a string.

Example text: "This is a message"

It will read only "This" into the variable message and no text after that.

Method III:

wchar_t *message[100];

This will define message as an array of 100 wide characters and a pointer to wchar_t. This method of initializing message works good. However, I am not sure if it is the right way. Because message in itself is pointing to the first character in the string. So, initializing it with the size, is it correct?

I wanted to understand it in more depth, the correct way of initializing a string. This same concept can be extended to a string of characters as well.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Neon Flash
  • 3,113
  • 12
  • 58
  • 96
  • 1
    You have shown no code that initializes the string, so we can't really help you except to say that method III is wrong. Can you show the initializations in addition to the variable declarations? Besides that, you don't need to cast the return value of `malloc` in C (method II) and you make a bit of a shaky statement about `message` being a pointer, when it's really an array (method I). – Carl Norum Apr 11 '13 at 14:38
  • 3
    Your third `message` method is 100 `wchar_t` *pointers*. Not sure if that is your intent, but your description following it clearly doesn't match that declaration. Likewise, it would probably do you well to understand the `wcscmp()`, `wcscpy()`, and other `wchar_t` function family members. There is no magic here. – WhozCraig Apr 11 '13 at 14:42
  • Possible duplicate of http://stackoverflow.com/questions/3112407/wchar-t-pointer – devnull Apr 11 '13 at 14:44

2 Answers2

36

The magic is the encoding-prefix L:

#include <wchar.h>

...

wchar_t m1[] = L"Hello World";
wchar_t m2[42] = L"Hello World";
wchar_t * pm = L"Hello World";

...

wcscat(m2, L" again");

pm = calloc(123, sizeof *pm);
wcspy(pm, L"bye");

See also the related part of the C11 Standard.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Why is it that anytime I use the `wchar_t * pm = L"Hello World";` I get the error `a value of type "const wchar_t *" cannot be used to initialize an entity of type "wchar_t"` has this been invalidated by the changes to C++ between C11 and C17? – Reahreic Jul 12 '23 at 14:38
  • @Reahreic: C is not C++. This answer refers to C. – alk Jul 20 '23 at 13:01
2

It really depends on what you want to do and how you use the data. If you need it globally, by all means, define a static array. If you only need it in a method, do the same in the method. If you want to pass the data around between functions, over a longer lifetime, malloc the memory and use that.

However, your method III is wrong - it is an array of 100 wchar_t pointers. If you want to create a 100 large wchar_t array and a pointer, you need to use:

wchar_t message[100], *message_pointer;

Also, concerning terminology: you are only declaring a variable in the method I, you never assign anything to it.

Yoon5oo
  • 496
  • 5
  • 11
Femaref
  • 60,705
  • 7
  • 138
  • 176