Why this code is printing string rather than printing address? [duplicate] - c++

This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 5 years ago.
Here in this code, cout<<q<<endl; is returning string "mani"?. q contain the address of first character 'm' so it should print address not string.please explain.
int main(){
char *q;
char b[5]={'m','a','n','i'};
q=&b[0];
cout<<b<<endl;
cout<<q<<endl;

std::cout has a special overload for const char*, which outputs the memory as an array of chars starting at the pointer passed up to the next NUL terminator (it's your job to make sure that the appropriate memory is available for that).
If you want to switch off this behaviour and output the pointer address then use a cast:
std::cout << (const void*)b << endl;

Related

Why does this results in a segmentation fault? [duplicate]

This question already has answers here:
can't modify char* - Memory access violation
(4 answers)
Closed 2 years ago.
int main()
{
char *p = "I like C++";
strcpy(p, "John Smith");
std::cout << p << std::endl;
EXIT_SUCCESS;
}
As the title stated, why does this results in segmentation fault
Speaking in the terms used by strcpy() documentation, you are trying to copy "John Smith" (the source argument) into p (the destination argument).
Although p is a pointer of type char *, it resides in the read-only data section (.rodata probably).
Trying to copy a new string into it, means trying to write over read-only memory.
Changing the declaration to char p[] = "I like C++"; fixes the issue as p now resides on the stach which is both readable and writeable memory.
By the way, the last line is missing a return and should be return EXIT_SUCCESS.

Is uninitialized char* a null pointer? [duplicate]

This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 7 years ago.
The code is:
char* c;
if(c!=NULL)
{
cout << "c has an address the address is "<<c;
}
else
{
cout << "c is null";
}
The output is:
c has an address the address is [Finished in 0.3s]
If c is not NULL, why c doesn't be printed out as an adress something like "0x401dee"
The problem with pointers is, that they often have a value after creating it, but its not a NULL pointer (!). But because its a pointer it points to a random address in your memory. That can cause pretty big problems, that's why you should ALWAYS initialize a pointer with
TYPE * NAME = NULL
So it hasn't a value and so it can't point to something, that could cause problems and now you can test with
NAME == NULL
(Of course you can also initialize it with a real value like the address of one of your variables)

Pointer dereferencing [duplicate]

This question already has answers here:
Why is address of char data not displayed?
(8 answers)
Closed 3 years ago.
char *m_chString="asd";
cout<<m_chString;//prints asd
cout<<*m_chString;//prints a
int nValue = 7;
int *pnPtr = &nValue;
cout<<*pnPtr;//prints 7
cout<<pnPtr;//prints the address of nValue
I gave two examples, in the first the pointer points to a string, and in the second, the pointer prints to an int value.
My question is, why cout<<m_chString; from the first example doesn't print the address of my string as it would do in the second example if I were to print pnPtr without dereferencing it?
Doesn't pnPtr point to an address?
The reason for that is that std::cout will treat a char * as a pointer to (the first character of) a C-style string and print it as such.
You can print the address by:-
cout << (void *) m_chString;
OR if you are great C++ fan then
cout << static_cast <const void *> (m_chString);

Hex values printing with different padding through char pointer in different instances [duplicate]

This question already has answers here:
Strange output when printing the value 0x89 (-119)
(3 answers)
Closed 8 years ago.
#include<stdio.h>
int main()
{
unsigned int a=0xabcdef12;
char *c=(char *)&a;
int i;
for(i=0;i<4;i++)
{
printf("%x.....%x\n",*c,c);
c++;
}
return 0;
}
O/p:
12.....bfad5bd4
ffffffef.....bfad5bd5
ffffffcd.....bfad5bd6
ffffffab.....bfad5bd7
If you see during first print, it is printing 12 but in all the subsequent prints it is printing correct values but padded with ffffff. Can someone explain this difference??
You are messing with pointers. Be careful out there. The line
char *bit_longer_name=(char *)&a;
says that "gimme pointer to CHAR type data array, and call it 'bit_longer_name'. The array initializes at variable 'a' address". Now when you do
bit_longer_name++
it actually travels that pointer forward in memory -- goes to next element in a array -- like my_char_array[i] does. The print you have there:
printf("%x.....%x\n",*c,c);
The first part prints the "value of the current array cell i am pointing to", and you are actually passing invalid value for the printf (you should get warning from that!), that causes the compiler to read over from the memory spot pointed by 'c' (-> its working in the first place).
The second part on the other hand is the "address i am pointing to with 'c'", and it travels forward as you can see.

passing string literal to function is not allowing to modify [duplicate]

This question already has answers here:
Segmentation fault reversing a string literal [duplicate]
(4 answers)
Closed 8 years ago.
Program 1:
void function(char arr[])
{
arr[0] = 'X';
printf("%s",arr);
}
int main()
{
function("MyString");
}
Output: Segmentation fault
Program 2:
int main()
{
char arr[] = "MyString";
arr[0] = 'X';
printf("%s",arr);
}
Output: XyString
What is the difference between program 1 and program 2? In prog1 also string value (not by reference) is passed to array so it should copy to array and allow to modify it? But it is throwing segmentation fault. In prog2 successfully allowing to change the arr[]. Why in prog1 it is not working?
Prog 1: You are passing a string literal, which is sent to the function as const char*. You can not change a const object.
Prog 2:You are using a non-const character array, which can be modified.
From your comment:
In prog1 also string value (not by reference) is passed to array so it should copy to array and allow to modify it?
Wrong. In program 1, the string literal is placed somewhere in memory (possibly in read only memory since it is constant), and then a const char* which points to that memory location is sent. The character array is not sent as parameter, only the address of first character.