Array address not visible [duplicate] - c++

This question already has answers here:
Printing array element memory adresses C and C++, why different output? [duplicate]
(4 answers)
Closed 6 years ago.
Refer the code below:
#include <iostream>
class Boy {
char name[10];
public:
void show() {
*name = 0;
std::cout << "\n" << &name[0];
}
};
int main() {
Boy b;
b.show();
}
Here, why don't we see the address of name[0]. I also tried with name, which itself is address. Still I can't see the address, it returns blank screen.

It's because you're using char* overload for operator<<, which treats the pointer as a pointer to c-string. Cast your pointer to void* to print it as such.
std::cout << "\n" << static_cast<void*>(&name[0]);

Related

& printing out the rest of a string instead of the address [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Printing C++ int pointer vs char pointer
(3 answers)
Closed 4 years ago.
Keep in mind that my knowledge of pointers is quite small, as I just started learning about them.
While I was messing around in C++, I wrote this small bit of code thinking it would just print out the address of each character in the string
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << &a[i] << endl;
}
return 0;
}
When I compiled and ran this, however, it resulted in it printing as if the string moved to the left.
It just doesn't make sense why when it uses &, which I thought would retrieve the address, would instead get the rest of the string.
As said in the comment, &a[i] is a pointer to a char, and << operator will print null terminated string starting from this character, not its address. So if you want to print the address, you must cast it to void *, as follow :
#include <iostream>
using namespace std;
string a = "Hello, World!";
int main() {
for(int i=0; i<a.length();i++) {
cout << (void *)&a[i] << endl; //cast to (void *) to get the address
}
return 0;
}
string subscript ([]) operator returns char. So & operation returns a pointer to char. And cout operator<< has an overloading for it, which consider it should print out the parameter as a c-string. You should cast it to void* so cout wouldn't think it is a string.
(void*)&a[i]

why are these NOT overloading functions? [duplicate]

This question already has answers here:
Passing an array as an argument to a function in C
(11 answers)
Closed 7 years ago.
I read here:
C: differences between char pointer and array
that char pointers and char arrays are not the same. Therefore, I would expect these to be overloading functions:
#include <iostream>
using namespace std;
int function1(char* c)
{
cout << "received a pointer" << endl;
return 1;
}
int function1(char c[])
{
cout << "received an array" << endl;
return 1;
}
int main()
{
char a = 'a';
char* pa = &a;
char arr[1] = { 'b' };
function1(arr);
}
Yet upon building I get the error C2084: function 'int function1(char *)' already has a body. Why does the compiler seem to consider a char pointer to be the same as a char array?
Because when you pass an array into a function, it magically becomes a pointer.
Your two functions, then, are the same.
The following are literally* identical:
void foo(int arr[42]);
void foo(int arr[]);
void foo(int* arr);
(* not lexically, of course :P)
This historical C oddity is the major reason lots of people mistakenly think that "arrays are pointers". They're not: this is just a bit of an edge case that causes confusion.

C++ - Null Pointers [duplicate]

This question already has answers here:
Why does std::cout output disappear completely after NULL is sent to it
(3 answers)
Closed 8 years ago.
I'm learning about pointers in C++. I wrote this simple program to show what I had a problem with:
#include <iostream>
using namespace std;
int main() {
cout << "test1";
char *ptr = 0;
cout << ptr;
cout << "test2";
}
When I run the program, it doesn't output "test2" at the end, instead only "test1". This should mean that it crashed when I tried to print out the value of ptr? I tried stepping through it in Eclipse debugger and it looks like every line gets executed but doesn't it throw an error or something?
char *ptr = 0;
cout << ptr;
There's an overload of the << operator that takes a char* operand, which it assumes is a pointer to a C-style string.
For pointer types other than char*, the << operator would print the value of the pointer (which is an address), but treating a null char* pointer as if it pointed to a C-style string causes undefined behavior. In any case, it's not going to print the pointer value.
To print the pointer value, you can convert it to void*:
cout << "test1\n";
char *ptr = 0;
cout << static_cast<void*>(ptr) << "\n";
cout << "test2" << "\n";;
Normally you can output a pointer to cout and it will print the address contained. However, when you output a char * it is interpreted as a C-style null-terminated string. In this case, it's a null pointer and does not point to a string.
Try casting it to a void * before outputting it.

Pointer in function parameters c++ [duplicate]

This question already has answers here:
C++ * vs [] as a function parameter
(3 answers)
Closed 9 years ago.
Consider this piece of code:
char strName[25];
void SetInfo(char *strName)
{
strncpy(m_strName, strName, 25);
}
Why are they using a pointer in the function parameter? Can't we just do this:
void SetInfo(char strName[]) {
strncpy(m_strName, strName, 25); }
? What is the difference between both?
Thank you
In this particular case, none at all (aside from one more letter to type). A char array "decays" to a pointer when passed to a function.
arrays always decay as pointers when passing as parameters.
in this case array is like a pointer (they point to the memory of the first element of the array)
void foo(char a[])
{
a[0] = '#';
cout << a[0];
cout << *a;
}
Both calls to cout prints the same character '#'.
void foo(char a[])
{
// gives you the size of a pointer to the array's data type.
// prints 4
sizeof(a);
// prints char *
cout << typeid(a).name();
}
I'm not sure if this is standard.

Printing a pointer with <iostream> [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 5 years ago.
Why does
#include <iostream>
using namespace std;
int main() {
cout << (char*)0x10 << endl;
}
segfault, but
#include <iostream>
using namespace std;
int main() {
cout << (void*)0x10 << endl;
}
seems to work just fine?
Because
cout::operator <<(void*)
prints a memory address, and
cout::operator <<(char*)
prints a null-terminated character array, and you run into undefined behaviour when you attempt to read the char array from 0x10.
The ostream::operator<< is overloaded, there is a version for char* which interprets the given pointer as a null-terminated string.
There's a special overload for << with char*, so that C-style strings can be output easily.
Thus
cout << (char*)0x10 << endl;
tries to print out the string located at (char*)0x10 which is not memory it's supposed to look at.