Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I print the address of first character?

Tags:

c++

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];
}
like image 512
Destructor Avatar asked Apr 22 '15 03:04

Destructor


People also ask

How do I print a character address?

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.

How do I print the first character of a string?

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.

How do you print the address of the first character of a string using C ++?

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.

How do you check if the first character of a string is a specific character?

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.


1 Answers

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);
}
like image 184
developerbmw Avatar answered Sep 25 '22 18:09

developerbmw