MMH MMH - 1 year ago 85
C Question

accessing the elements of a char*

I have a

char *p = "abcd"
, how can I access the elements
using only C(not C++)? Any help would be appreciated .

Answer Source

You can use indexing:

 char a = p[0];
 char b = p[1];
 /* and so on */

Equivalently you can use pointer arithmetic, but I find it less readable:

char a = *p;
char b = *(p+1);

If you really want to surprise someone you can also write this:

 char a = 0[p];
 char b = 1[p];
 /* and so on */
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download