I need to store delemeters in an array - is it better to do this:
char delem[3] = {'a', 'b', 'c');
or
char delem[3] = {97, 98, 99);
or
char delem[3] = {0x61, 0x62, 0x63);
Storing characters in an array
Started by John, Mar 03 2008 12:27 PM
3 replies to this topic
#1
Posted 03 March 2008 - 12:27 PM
|
|
|
#2
Posted 03 March 2008 - 12:48 PM
Do the one that best reflects the intended meaning. They are logically equivalent.
#3
Posted 03 March 2008 - 03:14 PM
Another common way to do it is:
Given the code above you can access a certain index like any other array, e.g. myDelims[0] == 'a'
If you are unsure of the size that the string is going to be, you might want to use the malloc() function to ensure enough space on the heap.
char *myDelims = "abc";
Given the code above you can access a certain index like any other array, e.g. myDelims[0] == 'a'
If you are unsure of the size that the string is going to be, you might want to use the malloc() function to ensure enough space on the heap.
-Dustin
www.theCprogrammer.com
www.theCprogrammer.com
#4
Posted 03 March 2008 - 10:06 PM
I just noticed you're switching between { and ), but I suspect it's a typo?
There's no "best way," and it's a matter of style, like WingedPanther pointed out. I would do it a little different though, but the result would be the same:
char delem[] = {'a', 'b', 'c'};
Like you see, you don't have to specify how many elements it's going to contain.
There's no "best way," and it's a matter of style, like WingedPanther pointed out. I would do it a little different though, but the result would be the same:
char delem[] = {'a', 'b', 'c'};
Like you see, you don't have to specify how many elements it's going to contain.


Sign In
Create Account

Back to top










