Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Char pointer to anonymous char array

Tags:

c

char

pointers

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

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

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? Thanks in advance...

like image 877
Soujanya Avatar asked Jul 30 '26 14:07

Soujanya


1 Answers

First of all char *three=(char*){'2','5','8','\0'}; is not valid C. To make a compound literal you must do like this:

char *three=(char[]){'2','5','8','\0'};

When that is fixed, then there is no difference in how you access the data. In both cases you can use the [] operator to access data, it can be used on any pointer type. Where the pointer points at doesn't matter.

like image 171
Lundin Avatar answered Aug 02 '26 08:08

Lundin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!