Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I output an individual character when using char *[] = "something"

Tags:

c++

char

pointers

I've been playing with pointers to better understand them and I came across something I think I should be able to do, but can't sort out how. The code below works fine - I can output "a", "dog", "socks", and "pants" - but what if I wanted to just output the 'o' from "socks"? How would I do that?

char *mars[4] = { "a", "dog", "sock", "pants" };

for ( int counter = 0; counter < 4; counter++ )
{
  cout << mars[ counter ];
}

Please forgive me if the question is answered somewhere - there are 30+ pages of C++ pointer related question, and I spent about 90 minutes looking through them, as well as reading various (very informative) articles, before deciding to ask.

like image 913
Azoreo Avatar asked Jan 18 '26 01:01

Azoreo


1 Answers

mars[i][j] will print the j'th character of the i'th string.

So mars[2][1] is 'o'.

like image 96
Alex Budovski Avatar answered Jan 20 '26 16:01

Alex Budovski



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!