If I have an definition as below,
TCHAR szANset[256][20];
then, I wanna initialize part of it LIKE these (from 1-9, a-z),
szANset['1'][] = {'`', '2', '\t', 'q', '\0'};
szANset['2'][] = {'1', '3', 'q', 'w', '\0'};
szANset['3'][] = {'2', '4', 'w', 'e', '\0'};
...
obviously, above is not working. but how I can program in a similiar way. Many thanks, guys.
6 2524 Banfa 9,065
Expert Mod 8TB
szANset['1'][] = {'`', '2', '\t', 'q', '\0'};
szANset['2'][] = {'1', '3', 'q', 'w', '\0'};
szANset['3'][] = {'2', '4', 'w', 'e', '\0'};
Arrays are indexed from 0 not from 1 (or 49 which is the value of the character '1'). You have to initialise the whole array or none and the initialisation is part of the definition not a separate thing.
Taking all this into consideration you need to write -
TCHAR szANset[256][20] = {
-
{'`', '2', '\t', 'q', '\0'},
-
{'1', '3', 'q', 'w', '\0'},
-
{'2', '4', 'w', 'e', '\0'}};
-
Which would initialise indexes 0, 1 and 2. The rest of the array would be set to 0. However these appear to be strings in which case you can do it this way too -
TCHAR szANset[256][20] = {
-
{"`2\tq"},
-
{"13qw"},
-
{"24we"}};
-
Some compilers will give you a warning if you do not provide enough initialisers for an array.
Arrays are indexed from 0 not from 1 (or 49 which is the value of the character '1'). You have to initialise the whole array or none and the initialisation is part of the definition not a separate thing.
That is not true anymore, starting from C99 you can use initialization designators as in -
int a[] = { 1, 2, [4]= 3, 4 };
-
This results in an array with elements 1, 2, 0, 0, 3, 4. The designator ['1'] would
be perfectly valid.
kind regards,
Jos
ps. gcc accepts quite a few other syntaxes for this as well.
Sorry, I didnt explain it clearly. About '1', I do mean it. Then, is this the only way I can do?
sprintf(szANset['1'], "%-s", "`2\tq");
...
Sorry, I didnt explain it clearly. About '1', I do mean it. Then, is this the only way I can do?
sprintf(szANset['1'], "%-s", "`2\tq");
...
Read my reply #3 again; doesn't it work?
kind regards,
Jos
TCHAR szANset[256][20];
then, I wanna initialize part of it LIKE these (from 1-9, a-z),
szANset['1'][] = {'`', '2', '\t', 'q', '\0'};
szANset['2'][] = {'1', '3', 'q', 'w', '\0'};
szANset['3'][] = {'2', '4', 'w', 'e', '\0'};
You can use TCHAR and then use char. Stuff like '2' is not valid when UNICODE is set. You would need a whar_t equivalant of '2'.
You need to use the TCHAR macros instead: -
TCHAR szANset[256][20];
-
-
-
szANset[_T(1)][] = TEXT(",2'\tq");
-
szANset[_T(2)][] = TEXT("13qw");
-
szANset[_T(3)][] = TEXT("24we");
-
szANset[_T(1)][] = TEXT(",2'\tq");
above doesnt work properly. But I've used _tcscpy_s instead. Many thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Fred Zwarts |
last post by:
If I am right, members of a class that are const and not static
must be initialized in the initialization part of a constructor.
E.g.
class C {
private:
const int I;
public:
C();
};
|
by: JKop |
last post by:
Okay here we go, I feel it's about time people conversed about the bullshit
aspects of C++ (including the bullshit stuff brought forward from C). I'll
begin with a few of my own grievances:
1)...
|
by: Charles Sullivan |
last post by:
Assume I have a static array of structures the elements of which
could be any conceivable mixture of C types, pointers, arrays.
And this array is uninitialized at program startup.
If later in...
|
by: questions? |
last post by:
say I have a structure which have an array inside.
e.g.
struct random_struct{
char name;
int month;
}
if the array is not intialized by me, in a sense after I allocated a
|
by: kk_oop |
last post by:
Hi. I recently wrote a simple little template that defines an array
that checks attempts to use out of bounds indexes. The only problem is
that it does provide the use array style value...
|
by: toton |
last post by:
Hi,
I can initialize an array of class with a specific class as,
class Test{
public:
Test(int){}
};
Test x = {Test(3),Test(6)}; using array initialization list. (Note
Test do NOT have a...
|
by: jamx |
last post by:
How can you initialize an array, in the initialization list of a
constructor ??
SomeClass
{
public:
SomeClass() : *init here* { }
private:
int some_array;
};
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and...
|
by: WaterWalk |
last post by:
Hello. When I consult the ISO C++ standard, I notice that in
paragraph 3.6.2.1, the standard states:
"Objects with static storage duration shall be zero-initialized before
any other...
|
by: Bill Reid |
last post by:
OK, let's say that have a function menu structure declaration
like this (since I basically do):
typedef struct function_item {
char *descrip;
void(*function)(void);
} t_function_item;
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |