convert unsigned long to char* in c++ [closed] - c++

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);
}

Related

C++ adding chars to a pointer [closed]

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 4 years ago.
Improve this question
From my understanding a string is just an array of char, so if i have a pointer reference to some char values shouldn't I be able to do:
*dest = "char"
However that doesn't work i have to do:
*dest = 'c';
*dest = 'h';
*dest = 'a';
*dest = 'r';
so if i have a pointer reference to some char values shouldn't I be able to do:
*dest = "char"
No, you shouldn't be able to do so.
*dest is a char. A single char object can only hold a single char object. A string is an array of characters. You cannot assign a string to a char.
However, if you had a pointer reference to some const char values, then you could assign the pointer like this:
dest = "char"
This would make the referred pointer to point to the string literal. However, this is different from *dest = 'c'; *dest = 'h'; .... The pointer assignment modifies the pointer and keeps the previously pointed characters unmodified, while assigning to the pointed character modifies the pointed characters, while keeping the pointer unmodified.
I'm guessing that by 'string' you ment a C-style string, so a char*
*dest = "char"
From what you said, dest is a char&*, when you dereference it, you get a char, type of "char" is const char*, so you're trying to assign a const char* to a char, which is a compile time error.
You should use std::string, which will enable you to do the assignment you described above, and also allocate and free memeory for you.
std::string dest = "char";

C++ unable to print the char variable [closed]

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;
}

How to pass "char* a" to "const char*"? [closed]

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 6 years ago.
Improve this question
Could you tell me the way to pass the value (char * a) to the value (const char* b)?
For example,
char * str = "Tokyo";
const char *;
"char = str" is OK?
char * str = "Tokyo";
In c++, this is not allowed †. You may not assign a const array (such as a string literal) to a non-const pointer.
const char *;
This is not allowed, because a variable declaration must have a name (unless it's a function argument).
const char *str = non_const_pointer_or_array;
The above is OK in c++.
† Implicit conversion from const to non-const is allowed in c but typically discouraged by compiler warnings. It was deprecated in c++ and not allowed at all since c++11.
In c++, it should just work. Native/Builtin types are automatically convertible to more const, but not to less const.
Therefore:
void foo(const char*) {
}
int main() {
char s[] = {'x', 'y', 'z', '\0'};
foo(s); //Compiles fine.
return 0;
}
This compiles fine, however the other way around, one has to use a const_cast. One should however not lie to the compiler and indicate that something is non const, while in actual fact it is const, as the program behavior in that case is undefined (one cannot know what the behavior of the program might be). const_cast is typically only used when old APIs require char* arguments despite it not modifying the arguments, and should never be used to "lie" to the compiler.
Example:
void some_old_api(char*){}
int main() {
const char* cs = "xyz";
some_old_api(const_cast<char*>(cs));
return 0;
}
A character array hard-coded in your program is a const char*, so you can declare it as const char* str = "Tokyo";Or, when you do have a char* str, you can cast it as follows: const char* myconst = (const char*) str;

Difference between char , char[] , char * [closed]

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 8 years ago.
Improve this question
I tried to set simple char with empty value without success. The main goal was to compare between std::string single char and pre-defined char
std::string str ="fcvfr";
char c = '' //trying to set empty char here .... but it gives me error
if(c == str[0])
{
//do something
}
This leads me to the question when should I use each of the following types:
char * , char , char[]
char represents a character (allocated on the stack). If you want to set it to empty, use char c = '\0' or char c = (char) 0.
char* cPtr is a pointer to a character. You can allocate an empty character on the heap with char* c = new char('\0').
char c[n] is a character array of size n.
Edit
As people have correctly pointed out below, a char is never empty, in the same sense as a container such as std::vector or std::string can be empty. A char is not fundamentally different to, say, an int, it's just shorter (1 byte as opposed to 2 or 4 or 8). Can an int be empty? Not as such; it can be zero, meaning that all its bits are set to zero in memory, and the same goes for a char. char c = '\0' will be represented as "00000000" on the stack.
A pointer to a char (char* cPtr), on the other hand can be 'empty' in the sense that it can point nowhere, by setting it to NULL. In this case, the pointer itself will exist on the stack and will contain a special sequence of 0/1's that your system interprets as NULL. Once you do cPtr = new char('\0'), a char (i.e. a byte) will be allocated on the heap and set to "00000000", and the value of cPtr on the stack will be changed to point to the address of the new character on the heap.
PS: don't actually do char* cPtr = new char('\0'), use an std::vector<char> or an std::string. Also, you may want to look into smart pointers.

char array initialization and some char string and c difference [closed]

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 8 years ago.
Improve this question
I see some code written like this:
char str[256] = {0};
or
char str[256] = {1};
when the former case, I use
printf("%s", str);
gives nothing,
Does it means give all the str[0] to str[256] all value 1?
when latterthe stdio give a ASCII smile char.
Furthermore, what is the difference between
char s[256] = {0};
printf("%c", s[1]);
It gives nothing in stdout
char s[256] = {0};
printf("%s", s[1]);
it give a (null)
I do not understand because I am a beginner of c char array and c pointers.
This initializes all 256 chars to 0:
char str[256] = {0};
This one initializes the first one to 1, and all the rest to 0:
char str[256] = {1};
Concerning the behaviour of printf, "%c" expects a single char. You pass it 0, which is NUL (the character string termination) so it prints nothing. "%s" expects a char* with the first character in a nul-terminated character string. You pass it a single char with value 0, which it interprets as a null pointer.