473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

about array initialization

50
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.
Jul 4 '08 #1
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

Expand|Select|Wrap|Line Numbers
  1. TCHAR szANset[256][20] = {
  2.     {'`', '2', '\t', 'q', '\0'},
  3.     {'1', '3', 'q', 'w', '\0'},
  4.     {'2', '4', 'w', 'e', '\0'}};
  5.  
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

Expand|Select|Wrap|Line Numbers
  1. TCHAR szANset[256][20] = {
  2.     {"`2\tq"},
  3.     {"13qw"},
  4.     {"24we"}};
  5.  
Some compilers will give you a warning if you do not provide enough initialisers for an array.
Jul 4 '08 #2
JosAH
11,448 Expert 8TB
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

Expand|Select|Wrap|Line Numbers
  1. int a[] = { 1, 2, [4]= 3, 4 };
  2.  
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.
Jul 4 '08 #3
leejwen
50
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");
...
Jul 5 '08 #4
JosAH
11,448 Expert 8TB
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
Jul 5 '08 #5
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. TCHAR szANset[256][20];
  2.  
  3.  
  4. szANset[_T(1)][] = TEXT(",2'\tq");
  5. szANset[_T(2)][] = TEXT("13qw");
  6. szANset[_T(3)][] = TEXT("24we");
  7.  
Jul 5 '08 #6
leejwen
50
szANset[_T(1)][] = TEXT(",2'\tq");
above doesnt work properly. But I've used _tcscpy_s instead. Many thanks
Jul 7 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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(); };
111
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)...
15
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...
30
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
3
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...
5
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...
15
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; };
4
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...
13
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...
14
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; ...
0
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,...
0
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$) { } ...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.