Jump to content

Storing characters in an array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
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);

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Do the one that best reflects the intended meaning. They are logically equivalent.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
broncoslb

broncoslb

    Learning Programmer

  • Members
  • PipPipPip
  • 34 posts
Another common way to do it is:
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.

#4
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
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.