How to Print the Address of Char Array

printing address of the character array

char *arr[4] is an array of 4 pointers to char. In other words it is array of 4 strings.
char *(*ptr)[4]=&arr declares a pointer to array of 4 pointers to char. The assignement in this line assigns the address of the first element of the array arr;
*(ptr)[i] dereferences the ith element of the ptr array.

I've just found a quiz on tricky C pointer declarations. You can practice there.

How do I get the address of elements in a char array?

std::cout << (void*) &charArray[0];

There's an overload of operator<< for char*, that tries to print the nul-terminated string that it thinks your pointer points to the first character of. But not all char arrays are nul-terminated strings, hence the garbage.

How to print the address of char array

You need to cast to void* to invoke correct overload of operator << instead of outputting as C strings

std::cout << static_cast<void*>(c) << std::endl;
std::cout << static_cast<void*>(c+3) << std::endl;

print address of array of char

cout << reinterpret_cast<void*>(c) << endl;

or just

cout << (void*)c << endl;

Unexpected output for address of char array

Cast a as a void* to avoid std::iostream::operator<<(const char*) being called:

#include<iostream>

int main()
{
char a[]={'0','1','2','3','4','5','6'};
std::cout << static_cast<void*>(a) << std::endl;
}

Please explain me this strange output for char array.

std::iostream::operator<<() exist for various argument types, each one is handled differently:

  • std::iostream::operator<<(const char*) prints a C-style string, this is what is called when you write std::cout << p.
  • std::iostream::operator<<(const void*) prints a pointer as an hexadecimal integer, this is what is called when you write std::cout << static_cast<void*>(a).

I had used a integer array which gives me a simple hexadecimal address as output.

If you'd declare an array of integers, printing it wouldn't call the const char* version but the const int* version if it existed. Since it's not defined, the compiler defaults to the const void* since a pointer can be implicitly cast to a const void*:

#include<iostream>

int main()
{
int a[]={'0','1','2','3','4','5','6'};
const int* p = a;
std::cout << p << std::endl; // prints address of a.
}

How to print an address of [x][y] element in 2D char array? (c++)

Yes, it's possible, but only by bypassing the IOstream special handling for char* (which assumes a C-string and formats its output accordingly):

cout << (void*)&arr[0][1] << endl;
// ^^^^^^^

This really has nothing to do with 2D arrays. Here's a much simpler example:

#include <iostream>

int main()
{
const char* str = "hi";
std::cout << &str[0] << '\n'; // "hi"
std::cout << (void*)&str[0] << '\n'; // some address
}

(live demo)

why an array reference variable cant hold address for char array in java?

For most objects, if you pass them to println, you get the normal toString() representation of the object. For arrays, it looks something like [C@6d4b1c02.

However, there is a version of println written specifically to accept a char array. So if you call that, you don't get the toString() representation of the array; you get the contents of the array, in this case ABC.

If you were to call the ordinary (non-char[]) version of println

System.out.println((Object) ch);

you would get the impenetrable [C@6d4b1c02 output.

How to print a char array in C through printf?

The code posted is incorrect: a_static and b_static should be defined as arrays.

There are two ways to correct the code:

  • you can add null terminators to make these arrays proper C strings:

    #include <stdio.h>

    int main(void) {
    char a_static[] = { 'q', 'w', 'e', 'r', '\0' };
    char b_static[] = { 'a', 's', 'd', 'f', '\0' };

    printf("value of a_static: %s\n", a_static);
    printf("value of b_static: %s\n", b_static);
    return 0;
    }
  • Alternately, printf can print the contents of an array that is not null terminated using the precision field:

    #include <stdio.h>

    int main(void) {
    char a_static[] = { 'q', 'w', 'e', 'r' };
    char b_static[] = { 'a', 's', 'd', 'f' };

    printf("value of a_static: %.4s\n", a_static);
    printf("value of b_static: %.*s\n", (int)sizeof(b_static), b_static);
    return 0;
    }

    The precision given after the . specifies the maximum number of characters to output from the string. It can be given as a decimal number or as * and provided as an int argument before the char pointer.

C++ int and char array adress

For starters the program has undefined behavior because the declared character array

char char_array [5] {'a', 'e', 'i', 'o', 'u'};

does not contain a string but using the overloaded operator << in these statements

cout << char_array << endl;
cout << char_array+1 << endl

for a pointer to char requires that the pointer would point to a string.

You could at least declare the array like

char char_array [6] {'a', 'e', 'i', 'o', 'u', '\0' };

Using an integer array as an expression in the operator << results that the overloaded resolution selects the operator for the type void * and the operator outputs the address of the first element of the integer array.

In these statements

cout << char_array+1 << endl;
cout << int_array + 1 << endl;

there is used the pointer arithmetic. The expression char_array+1 or int_array + 1 increases the value or the pointer (the array designator in such an expression is implicitly converted to pointer to its first element) by the value equal to sizeof( char ) or sizeof( int ) correspondingly.

sizeof( char ) is always equal to 1. sizeof( int ) depends on the used system and usually at least for 32-bit systems is equal to 4. And this output

0x61fe00
0x61fe04

demonstrates this.

If you want to output addresses for elements of the character array then you should write for example

cout << static_cast<void *>( char_array ) << endl;
cout << static_cast<void *>( char_array+1 ) << endl;


Related Topics



Leave a reply



Submit