Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
#include <iostream>
#include <ncurses.h>
using namespace std;
int main()
{
char ch[10];
ch = getch();
cout << ch;
}
I'm getting the following error message:
incompatible types in assignment of ‘int’ to ‘char [10]’
ch=getch();
C++ unable to print the char variable
char ch[10];
ch is not a "char variable". It is a char array variable. Array variables can not be assigned to.
Perhaps you intended to have a char variable instead:
char ch;
The value of a char can be assigned.
getch() returns int (or char). You declared:
char ch[10];
which is a character array not a single character that cannot be cast to an int. This throws an error.
It can be corrected by changing:
char ch[10];
to:
char ch;
By changing it, you are declaring a character ch instead of a character array.
ch[10] is a character array, it's not a single character. getch() read a single byte character from input.so try following code.
int main()
{
char ch;
ch = getch();
cout << ch;
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
#include<iostream>
using namespace std;
char* str[]={"Man","Woman","Car","Plane",0};
int main(){
char** cp=str;
while(*cp!=0)
cout<<*cp++<<endl;
return 0;
}
It prints the String.
But when I print **cp++ I get only first letters like M,W,C,P.
For starters the array should be declared with the qualifier const because in C++ string literals have types of constant character arrays.
const char* str[]={"Man","Woman","Car","Plane",0};
In fact the declaration above is equivalent to
const char* str[]={ &"Man"[0], &"Woman"[0], &"Car"[0], &"Plane"[0], 0 };
because the string literals having array types used as initializers in this declaration are implicitly converted to pointers to their first elements.
In this declaration
char** cp=str;
that also should be written like
const char** cp=str;
the pointer cp points to the first element of the array that has the type char * and points to the first character of the string literal "Man".
Dereferencing the pointer cp one time like *cp you will get the first element of the array that has the pointer type char * and points to the character 'M' of the string literal "Man". Dereferencing the pointer the second time like **cp you will get an object of the type char that contain this character 'M'.
This question already has answers here:
cout << with char* argument prints string, not pointer value
(6 answers)
Closed 2 years ago.
I have written a simple C++ code, and its working fine. But I don't know how it is working. I am just replacing "l" with "r" using myfun().
The return type of myfun() is char*. If I am returning &(str[0]), that is, only the address of the first element of the array, then why is it printing the complete string "herloworld"? Please explain what return &(str[0]); is doing.
#include <iostream>
using namespace std;
char* myfun(char str[])
{
str[2] = 'r';
return &(str[0]);
}
int main()
{
char str[] = "helloworld";
char* word;
word = myfun(str);
cout << word;
}
The operator << is overloaded for the type char * such a way that it expects that the used pointer of the type char * points to the first character of a string.
So the operator outputs all characters of the passed string until the zero character '\0' is encountered.
Also pay attention to that arrays used in expressions with rare exceptions are converted to pointers to their first elements.
So this call
word = myfun(str);
is equivalent to
word = myfun( &str[0]);
On the other hand, a function parameter having an array type is implicitly adjusted to pointer to the element type.
So this function declaration
char* myfun(char str[]);
is equivalent to the following declaration
char* myfun(char *str);
The both declarations declare the same one function.
And within the function instead of this return statement
return &(str[0]);
you could write
return str;
Correspondingly in main you could write
cout << myfun(str);
without declaring and using the intermediate pointer word.
This question already has answers here:
What do single quotes do in C++ when used on multiple characters?
(5 answers)
Closed 6 years ago.
During learning about C++ by reading a book I have seen this (for me) strange line of code.
char ch('AB'); // Or char ch = 'AB';
It is strange for me because I don't get that you can assign to a char multiple "letters" without getting any exception.
cout << "Characters in ch: " << ch << endl; // Output B
Why does this work? And how is it working internally? Is it only saving the last character and ignoring the other ones?
It's called a multicharacter literal which are completely valid C++:
Multicharacter literal, e.g. 'AB', has type int and implementation-defined value.
[...]
Many implementations of multicharacter literals use the values of each char in the literal to initialize successive bytes of the resulting integer, in big-endian order, e.g. the value of '\1\2\3\4' is 0x01020304.
It is strange for me because I don't get that you can assign to a char multiple "letters" without getting any exception.
You should see it as a type conversion (demo):
#include <iostream>
using namespace std;
int main()
{
{
int i = 'abcd';
char c = i; // cast form int to char -> c == 'd'
cout << c; // prints 'd'
}
{
char c = 'abcd'; // cast form int to char -> c == 'd'
cout << c; // prints 'd'
}
return 0;
}
The order in which the characters are stored in int is not specified by standard. However, a well designed compiler will consider endianness when storing a multi-character constant: GCC and VisualC behaves the same way.
This question already has answers here:
Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?
(19 answers)
Closed 8 years ago.
problem is in 15 and 16th line
i am unable to store char into
a pointer pointing to a string.
the problem is same as given in
my book.?do i need to change my
compiler dev c++?
plz help.
#include<iostream>
#include<conio.h>
using namespace std;
void reverse(char *str)
{
char *end=str;
char *beg=str;
char temp;
while(*end)
{
end++;
}
end--;
while(beg<end)
{
cout<<*beg<<" , "<<*end<<endl;
temp=*beg;
*beg=*end;
*end=temp;
beg++;
end--;
}
cout<<str;
}
int main()
{
char *str="saurabh";
reverse(str);
getch();
return 0;
}
char *str="saurabh";
You cannot manipulate "saurabh" because it is literal.
For this you should either copy it to char[],
Example,
char arr[20];
char *ptr = "Data";
strcpy(arr,ptr);
I know that you likely need to implement yourself reverse, but you can use std implementation for tests:
std::string s("abc");
std::reverse(s.begin(), s.end());
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I cast unsigned long to char*.
I am doing the following for loop
for (unsigned long i = 1; i< 7;i++)
{
callAMethod(5,i);
}
The method definition is as follows
callAMethod(int, const char*)
While I do this I get the following error:
invalid conversion from `unsigned long' to `const char*'
How do I do this?
Old C pointer arithmetic:
for (unsigned long i = 1; i< 7;i++)
{
callAMethod(5, (const char*)(&i));
}
But this way you'll just get one byte of the 8 bytes of the long. Do you want to get the string representation of the long? You may utilize STL and get the str().
"const char*" means "address of a character". You're going to pass it values 1 <= n < 7.
You can ask the compiler to pretend they are valid pointer-to-char values by casting
callAMethod(5, reinterpret_cast<const char*>(i));
But then I'd hazard a guess that your application will crash when it trys to read the memory at address "1" to look for a char
It seems like your "callAMethod" is expecting a C-style string, which is an arbitrary sequence of characters with the end-of-string denoted by a character of value 0:
const char hello[6] = { 'h', 'e', 'l', 'l', 'o', 0 };
being equivalent to
const char* hello = "hello";
(when you write a string in quotes like that in C/C++, the compiler automatically adds a 0 byte to the end of the stored string).
Typically, when you pass strings to a function, the finger print is "const char*"
So what you actually want to know is "how do I make a string from a long value" and the answer is "printf".
char str[20];
for (unsigned long i = 1; i < 7; ++i) {
#ifdef _MSC_VER
sprintf_f(str, "%u", i);
#else
snprintf(str, sizeof(str), "%u", i);
#endif
callAMethod(5, str);
}