Why the entire string is displayed as outcome? Why address of 1st character not getting printed? How can I print address of 1st character? Please help me.
#include <iostream>
int main()
{
char x[6]="hello";
std::cout<<&x[0];
}
when you have to print the address of character, then & (Address of) operator fails to do so. cout<<(void*)&c; will produce the desired address.
Get the first character of a string in python As indexing of characters in a string starts from 0, So to get the first character of a string pass the index position 0 in the [] operator i.e. It returned a copy of the first character in the string. You can use it to check its content or print it etc.
To access the first character of a string, we can use the subscript operator [ ] by passing an index 0 . Note: In C++ Strings are a sequence of characters, so the first character index is 0 and the second character index is 1, etc.
Using the isDigit() method Therefore, to determine whether the first character of the given String is a digit. The charAt() method of the String class accepts an integer value representing the index and returns the character at the specified index.
The <<
operator on std::cout
will treat a char*
as a null-terminated string. You will need to cast it to a void*
to print the pointer value.
Try this:
#include <iostream>
int main()
{
char x[6] = "hello";
std::cout << static_cast<void*>(x);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With