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
6 2844
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.
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?
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: -
enum Value {NONE, ODD, EVEN};
-
-
if (something == ODD)
-
{
-
//etc...
-
}
-
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.
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
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: -
char *Parity[3] = {"None", "Odd", "Even"};
-
Aah, now I understand!
Thank you all.
Sign in to post your reply or Sign up for a free account.
Similar topics
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:
|
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.
|
by: Hans |
last post by:
Hello,
Why all C/C++ guys write:
const char* str = "Hello";
or
const char str = "Hello";
|
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...
|
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....
|
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)
...
|
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.
|
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...
|
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...
|
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...
|
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: 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: 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,...
|
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...
|
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...
|
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...
|
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...
| |