How to create a static array of literals

in
Platforms:

Defining an array of text string is a basic task that most developer need to do. This is simple on most environment... but not so trivial in Symbian OS where you have to deal with descriptors and literals.

By default, you may want to try an easy

_LIT(KSomeText,"Hello");
_LIT(KMoreText,"World");

const TPtrC gStringArray[] =
{
  KSomeText,
  KMoreText,
  ...
};

Unfortunately, this will result in a friendly:

error: conversion from `const TLitC<x>' to non-scalar type `TPtrC16' requested

A literal not being a descriptor, you cannot create your array in this way. At least, not exactly in this way. The solution is however simple to implement - not always to find - as the () operator applied to a literal convert it into a descriptor. So just fix the code above and here you are:

_LIT(KSomeText,"Hello");
_LIT(KMoreText,"World");

const TPtrC gStringArray[] =
{
  KSomeText(),
  KMoreText(),
  ...
};


Re: How to create a static array of literals

Nice article Eric. Must be a "const TPtrC" Eye-wink

Re: How to create a static array of literals

Well spotted! TPrc is indeed not a Symbian class (and now I have a proof that someone is reading my tutorials Eye-wink )
Thanks.

Re: How to create a static array of literals

A pretty useful tip. Thanks!

Re: How to create a static array of literals

What's the proper way to create a const TUid array? Or even a const TInt array for starters. What's the best way to find out the number of items in any of these arrays?

g-string array... he he he, snort! Sticking out tongue

Re: How to create a static array of literals

Laughing out loud Smiling helped me good