Why does the following code produce the output "h"? I do not understand it. Since it's dereferencing it, shouldn't it print out its memory address?
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << *("hello");
return 0;
}
"hello" evaluates to a pointer to the first character of the string , dereferencing it evaluates to that character.
A string literal ("hello" in this case) is a array of const char of size N where N is the number of characters plus a null terminator. That array can decay to a pointer to the first element. When you dereference that pointer you now have the first element of the array, which is a character. That is why h is printed as you gave cout a character.
The string is saved at some memory location in the binary (when the source is compiled).
A string like "hello" is converted to a char * (pointer to char). Therefore when you dereference it, it will get you the first char of your "string".
Related
I am new to C++. As per my knowledge in the above case names[0] should be the index of 'R'.
I guess I am missing out or lacking knowledge.
Please help me.
#include<iostream>
using namespace std;
int main() {
char *names[] = {
"Rohan",
"Sammy",
"Samuel",
"Henil"
};
// Expected R to be printed and not Rohan
cout << *names << endl;
return 0;
}
Output:
Rohan
You just need to put brackets *names[0]
[CODE]:
#include<iostream>
#include<string>
using namespace std;
int main(){
char* names[] = {
"Rohan",
"Sammy",
"Samuel",
"Henil"
};
// Expected R to be printed and not Rohan
cout<<*names[0]<<endl;
return 0;
}
[RESULT]:
R
Live demo here!
As per my knowledge of in the above case names[0] should be the index of 'R'
names[0] is not an index. It is a pointer. It points to the char R within the string literal.
When a pointer is inserted into a character stream, the behaviour is not to stream the pointed object.
When you insert a pointer to char into a character stream, it is assumed to be a pointer to null terminated string and the behaviour is to streamed the entire string. You should adjust your expectations accordingly.
P.S. The program is ill-formed since C++11 because string literals are not implicitly convertible to char*.
Referring to an array by name will decay to a pointer to the 1st element of the array. So, names is the same as &names[0], and thus *names is the same as names[0], which is the 1st char* element in the array, not the 1st char of the 1st string in the array.
Going by the books, the first cout line should print me the address of the location where the char variable b is stored, which seems to be the case for the int variable a too. But the first cout statement prints out an odd 'dh^#' while the second statement correctly prints a hex value '
ox23fd68'. Why is this happening?
#include<iostream>
using namespace std;
int main()
{
char b='d';
int a=10;
char *c=new char[10];
c=&b;
int *e=&a;
cout<<"c: "<<c<<endl;
cout<<"e: "<<e;
}
There is a non-member overload operator<<(std::basic_ostream) for the const char* type, that doesn't write the address, but rather the (presumed) C-style string1). In your case, since you have assigned the address of a single character, there is no NUL terminator, and thus no valid C-style string. The code exhibits undefined behavior.
The behavior for int* is different, as there is no special handling for pointers to int, and the statement writes the address to the stream, as expected.
If you want to get the address of the character instead, use a static_cast:
std::cout << static_cast<void*>( c ) << std::endl;
1) A C-style string is a sequence of characters, terminated by a NUL character ('\0').
Actually this program has problem. There is a memory leak.
char *c=new char[10];
c=&b;
This allocates 10 characters on heap, but then the pointer to heap is overwritten with the address of the variable b.
When a char* is written to cout with operator<< then it is considered as a null terminated C-string. As the address of b was initialized to a single character containing d op<< continues to search on the stack finding the first null character. It seems the it was found after a few characters, so dh^# is written (the d is the value of variable b the rest is just some random characters found on the stack before the 1st \0 char).
If you want to get the address try to use static_cast<void*>(c).
My example:
int main() {
char *c;
char b = 'd';
c = &b;
cout << c << ", " << static_cast<void*>(c) << endl;
}
An the output:
dÌÿÿ, 0xffffcc07
See the strange characters after 'd'.
I hope this could help a bit!
I am learning pointers and i tried this following program
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
char* getword()
{
char*temp=(char*)malloc(sizeof(char)*10);
cin>>temp;
return temp;
}
int main()
{
char *a;
a=getword();
cout<<a;
return 0;
}
To my level of understanding, a is a pointer to a character, and in the function getword() I returned temp which I think the base &temp[0]. I thought that the output would be the first character of the string I enter, but I got the entire string in stdout. How does this work?
In the tradition of C, a char* represents a string. Indeed, any string literal in your program (e.g. "hello") will have a type of const char *.
Thus, cout::operator<<( const char * ) is implemented as a string-output. It will output characters beginning at the address it is given, until it encounters the string terminator (otherwise known as null-terminator, or '\0').
If you want to output a single character, you need to dereference the pointer into a char type. You can choose one of the following syntaxes:
cout << *a; // Dereference the pointer
cout << a[0]; // Use array index of zero to return the value at that address
It should be noted that the code you provided isn't very C++ish. For starters, we generally don't use malloc in C++. You then leak the memory by not calling free later. The memory is uninitialised and relies on cin succeeding (which might not be the case). Also, you can only handle input strings of up to 9 characters before you will get undefined behaviour.
Perhaps you should learn about the <string> library and start using it.
It's true that char* "points to a character". But, by convention, and because with pointers there is no other way to do so, we also use it to "point to more than one character".
Since use of char* almost always means you're using a pointer to a C-style string, the C++ streams library makes this assumption for you, printing the char that your pointer points to … and the next … and the next … and the next until NULL is found. That's just the way it's been designed to work.
You can print just that character if you like by dereferencing the pointer to obtain an actual char.
std::cout is an overloaded operator and when it receives a char * as an operand then it treats it as a pointer to c style string and it will print the entire string.
If you want to print the first character of the string then use
cout << *a;
or
cout << a[0];
In your code, std::cout is an ostream and providing a char* variable as input to operator<< invokes a particular operator function overload to write characters to the ostream.
std::ostream also has a operator overload for writing a single character to itself.
I'm assuming you now know how to dereference a char* variable, but you should be using std::string instead of an unsafe char* type.
Here is the correct code
#include <stdio.h>
#include <stdlib.h>
char* getword()
{
char*temp=(char*)malloc(sizeof(char)*10);
scanf("%s",temp);
return temp;
}
int main()
{
char *a;
a = getword();
int currChar = 1;
printf("%c",*(a + currChar)); //increment currChar to get next character
return 0;
}
I have some code:
#include <iostream>
#include <string>
using namespace std;
int main(){
char abc [20] = "Hello Hello Hi";
char* ptr = abc;
cout << (abc+3);
return 0;
}
Why does it print out from the third character up and not simply the third character?
-edit- to whoever flagged it down. it's not the same as prinf(), but same type of concept. i just didnt know the nuances
To understand why, you have to understand a bit of pointer arithmetic.
abc is the same as &abc[0] and (abc + 3) is the same as &abc[3]
that being said, cout prints a string from a given char * to a null character.
Therefore, you are basically just printing the string that starts at the third character to the end of the string. If you wanted to print just the third character, you could dereference the pointer to the third character like this.
*(abc + 3)
char abc [20] decays to char*, which is effectively a c-style string. abc+3 remains a pointer, just offset from abc[0], so std::cout will still print it as a string.
Because streams have an overload for char* that treats the input like a C-String. The stream operators will print everything until a null terminator (when presented with a char*).
If you want to print a single character then you have to convert your expression to be char not char*.
cout << (abc+3); // Type of expression is char*
cout << (*(abc+3)); // Type of expression is char
// This prints a signle character.
I've a code
#include <iostream>
using namespace std;
void foo(char *name){ // ???
cout << "String: " << name << endl;
}
int main(){
foo("Hello");
return 0;
}
I don't know why I use "char name" won't work. Please help.
Cheers,
char a is just a single character, while char* is a pointer to a sequence of characters - a string.
When you call foo("Hello"), you pass in a string literal (strictly speaking an array of chars) which is convertible to a pointer to char. Therefore foo must accept char* rather than char because otherwise the types wouldn't match.
char name is a single character
char* name is a pointer to a character in heap and if allocated correctly, can be an array of characters.
A string can be represented as an array of char(s). You can use the char * pointer to refer to that string.
You could use char name with foo('c'), because that's a char.
Because a simple char is just one character, like 'c','A','0',etc.. char* is a pointer to a region in memory, where one or more chars are stored.
In C and C++ strings are quite often represented as an array of characters terminated by the null character.
Arrays in C and C++ are often represented as a pointer to the first item.
Therefore a string is represented as a pointer to the first character in the string and that is what char *name means. It means name is a pointer to the first character in the string.
You might want to read up a bit on pointers, arrays and strings in C/C++ as this is fundamental stuff!
char refers to a single character. char* is a pointer to an address in memory that contains a 1 or more characters (a string). If you are using C++, you might consider using std::string instead as it may be slightly more familiar.