473,461 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Const string arrays

8
Can you please help me understand why I do not see the answer expected (Parity: Odd). Can I have an array of strings as constants? How do I index each string in the array?

const Parity[3] = {'None', 'Odd', 'Even'};
char string[];

sprintf(string, "Parity: %s", Parity[1]);
printf(string);

Thanks
Aug 23 '07 #1
6 2844
RRick
463 Expert 256MB
You got the right idea but your code has syntax problems. Does this compile for you? I doubt it.

Your Parity definition says its a "const Parity" but it does not tell the compiler what it is ( i.e. int, char, pointer to something?) Put that something between the const and Parity. Also, use the double quote instead of the single quote for strings. The single quote defines a single char, not a string.

Finally, your string variable needs some size. Pick a value bigger than the largest string in Parity.
Aug 24 '07 #2
Toe001
8
Thank you for the help.

Yes it did compile without errors (it is a Z8 micro compiler), so this is why I was confused!

I have followed your suggestions and changed to:

const char Parity[3][4] = {"None", "Odd", "Even"};
char string[];

sprintf(string, "Parity: %s", Parity[1]);
printf(string);

And it now seems to work. However, this does not seem very elegant to have to use a multi-dimension array to hold these three constants (strings), is there a better way? Maybe I should I be using ennumerated types rather?
Aug 24 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
If you don't need the items as strings, then an enum is the way to go.

All an enum is a list of named integer values. You use enums to avoid hard-coded values in your program:
Expand|Select|Wrap|Line Numbers
  1. enum Value {NONE, ODD, EVEN}; 
  2.  
  3. if (something == ODD)
  4. {
  5.   //etc...
  6. }
  7.  
Since an enum is a named integer value and not an actual integer, an enum is a declaration and that means you can put it in a header file and use it in all of your source files.
Aug 24 '07 #4
Toe001
8
Thank you for this further explanation of enum types. It seems a better way to go about things and I will try this.

For my education - and I am a beginner - can one not have in C a simple array with three elements to hold three strings, and so as not to allow these strings to be altered, add the qualifier "const". Something like:

const char Parity[3] = {"None", "Odd", "Even"};

It seems straight forward, but does not work?

I would expect Parity[1] to hold the string "Odd" etc.

Thanks
Aug 24 '07 #5
ilikepython
844 Expert 512MB
Thank you for this further explanation of enum types. It seems a better way to go about things and I will try this.

For my education - and I am a beginner - can one not have in C a simple array with three elements to hold three strings, and so as not to allow these strings to be altered, add the qualifier "const". Something like:

const char Parity[3] = {"None", "Odd", "Even"};

It seems straight forward, but does not work?

I would expect Parity[1] to hold the string "Odd" etc.

Thanks
Each element is a char so 1 letter. You need an array of char *s:
Expand|Select|Wrap|Line Numbers
  1. char *Parity[3] = {"None", "Odd", "Even"};
  2.  
Aug 24 '07 #6
Toe001
8
Aah, now I understand!

Thank you all.
Aug 24 '07 #7

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
5
by: Minti | last post by:
How do we initialize static const data in C++, I tried class Foo { static const std::string name = "MyName"; }; I don't see any reason as to why this must not work.
23
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
4
by: George Ter-Saakov | last post by:
I need to declare const 2 dimensional array something like string topmenu = { {"menu1", "aaa"}, {"menu2", "bbb"} } Thanks. George.
3
by: Henryk | last post by:
I'm using the gcc with the -pedantic setting. On my embedded system I need a parameter class that contains all setting for the software. This class needs to be ROM-able to locate it in some kind...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.