Hi all,

I was wondering how to access individual elements in the below case:

char *three=(char*){'2','5','8'};

If the assignment were like this:

char *three="258";

it can be accessed with three[0],three[1].....etc. How to access in the first case?

You have a couple of problems. Both examples are REALLY bad and probably won't work.

Technique #1
char three[] = { '2', '5', '8', '\0');
You can use three as a char*.

Technique #2
Your example won't work because "258" is a const char* and NOT a char*, so the results are not modifiable. If you make three a const char* then it's contents will not be modifiable. If you don't want to modify the string, then your second example would work if it is a const char*.

Note that in my Technique #1 I added a terminating NULL char so that the string is properly terminated. Using "258" does that for you automatically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.