how to convert char* pointer address to int in c++ - c++

how can i convert a char* address to int?
in cygwin, i've got the error as follow:
test.cpp:31:80: 错误:从‘char*’到‘int’的转换损失精度 [-fpermissive]
cout << "hex:0x" << setw(8) << left << hex << reinterpret_cast(&pchar[i])
(translate: Error, the conversion from ‘char*’ to ‘int’ will lose precision)
following is my source code:
int main()
{
int num = 0x12345678;
char *pchar = reinterpret_cast<char*>(&num);
if (0x12 == *pchar)
{
cout << "big-end" << endl;
}
else if (0x78 == *pchar)
{
cout << "little-end" << endl;
}
else
{
cout << "wtf" << endl;
}
for (int i = 0; i < 4; ++i)
{
cout << "hex:0x" << setw(8) << left << hex << reinterpret_cast<int>(pchar + i)
<< "val:0x" << hex << static_cast<int>(pchar[i]) << endl;
}
return 1;
}

You can't: the behaviour would be undefined. This is because a char* is unrelated to an int.
In your case why not use %p as the format specifier for the pointer? (Strictly speaking you should convert the argument to a void* or const void*).
cout does this automatically for you:
cout << (void*)(pchar);

Related

c++ keep getting fpermissive error in function calls

I keep getting an fpermissive error in my code where i call the functions im not sure why. the two errors are:
error: invalid conversion from ‘char*’ to ‘char’ [-fpermissive]
132 | playGame(winnerWord, wordArray);
error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
90 | buildResult(guess, winWord);
Im pretty new to C++ so im not sure what to do, any help is greatly appreciated.
void playGame(char winWord, string arr[2315]) {
string guessWord;
cout << "Ok. I am thinking of a word with 5 letters." << endl;
cout << "What word would you like to guess?" << endl;
getline(cin, guessWord);
while (verifyExists(guessWord, arr) == 2)
{
cout << "The word: " << guessWord << " is not in the word list" << endl;
getline(cin, guessWord);
}
while (verifyExists(guessWord, arr) == 3)
{
cout << "You must enter a word that is 5 letters in length: " << endl;
getline(cin, guessWord);
}
cout << guessWord << endl;
char guess[5];
for (int i = 0; i < 5; i++)
{
guess[i] = guessWord[i];
}
buildResult(guess, winWord);
}
int main() {
string wordArray[2315];
ifstream myfile ("proj1_data.txt");
cout << " Welcome to UMBC Wordle" << endl;
if (myfile.is_open())
{
string word;
int loop = 0;
while (getline(myfile, word))
{
wordArray[loop++] = word;
}
cout << " " << endl;
cout << " Your file was imported!" << endl;
cout << " 2315 Words imported" << endl;
cout << " " << endl;
myfile.close();
}
srand(time(0));
string chosenWord = wordArray[rand() % 2315];
char winnerWord[5];
for (int i = 0; i < 5; i++)
{
winnerWord[i] = chosenWord[i];
}
playGame(winnerWord, wordArray);
return 0;
}

Cannot print buffer address

I want to test the following code, however I get a compilation error. The thing that confuse me is that the way and create and print pd1 and pd2 are the same as pd3 and pd4, but the compiler complains about pd3 and pd4 when I print.
const int BUF = 512;
const int N = 5;
char buffer[BUF]; // chunk of memory
int main(){
using namespace std;
double *pd1, *pd2;
int i;
cout << "Calling new and placement new:\n";
pd1 = new double[N]; // use heap
pd2 = new (buffer) double[N]; // use buffer array
for (i = 0; i < N; i++)
pd2[i] = pd1[i] = 1000 + 20.0*i;
cout << "Buffer addresses:\n" << " heap: " << pd1 << " static: " << (void *)buffer << endl;
cout << "Buffer contents:\n";
for(i = 0; i < N; i++) {
cout << pd1[i] << " at " << &pd1[i] << "; ";
cout << pd2[i] << " at " << &pd2[i] << endl;
}
cout << "\nCalling new and placement new a second time:\n";
double *pd3, *pd4;
pd3 = new double[N];
pd4 = new (buffer) double[N];
for(i = 0; i < N; i++)
pd4[i] = pd3[i] = 1000 + 20.0 * i;
cout << "Buffer contents:\n";
for (i = 0; i < N; i++) {
cout << pd3[i] < " at " << &pd3[i] << "; ";
cout << pd4[i] < " at " << &pd4[i] << endl;
}
return 0;
}
Compilation error:
newplace.cpp: In function ‘int main()’:
newplace.cpp:33:36: error: invalid operands of types ‘const char [5]’ and ‘double*’ to binary ‘operator<<’
cout << pd3[i] < " at " << &pd3[i] << "; ";
^
newplace.cpp:34:36: error: invalid operands of types ‘const char [5]’ and ‘double*’ to binary ‘operator<<’
cout << pd4[i] < " at " << &pd4[i] << endl;
You missing one < symbol here
cout << pd3[i] < " at " << &pd3[i] << "; ";
cout << pd4[i] < " at " << &pd4[i] << endl;
Try
cout << pd3[i] << " at " << &pd3[i] << "; ";
cout << pd4[i] << " at " << &pd4[i] << endl;
You only put one < in the stream where you are trying to print out the buffer contents.
cout << pd3[i] < " at " << &pd3[i] << "; "; // there is only one <
cout << pd4[i] < " at " << &pd4[i] << endl; // ^
Make sure you have two <'s in the stream insertion operator.
cout << pd3[i] << " at " << &pd3[i] << "; ";
cout << pd4[i] << " at " << &pd4[i] << endl;

Converting fl in hexadecimal in c++

I am new to C++, and programming, and I want to write a C++ program to convert a float in hexadecimal with the help of pointers
I've looked on other threads and really tried to get a hold of this but can't seem to figure it out.
Here is what I have done so far:
int main()
{
float number = -12.0
unsigned char *ptr = ((unsigned char*) &number);
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte " [i] << "is : " << ptr[i] << endl;
}
So with this, i assume I am able to have access to the bytes that compose the float. However, can you suggest any ideas to convert this to hexadecimal. I guess, I have to be able to read the binary behind all this... but I am not really sure how.
Note: I understand how to convert from binary to hexadecimal
iostreams can print numbers in hexadecimal:
int main() {
float number=-1.0;
unsigned char *ptr = ((unsigned char*)&number);
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte ";
cout << setw(0) << dec << i << "is : ";
cout << setw(2) << setfill('0') << hex << ptr[i] << endl;
}
what you want might be sth like this:
float number=-1.0;
char cz[]="0123456789ABCDEF";
int si=sizeof(float);
char* ptr=(char*)(&number);
for(int i=0;si-i;++i){
std::cout<<cz[((*ptr)&0xf0)>>4]<<cz[(*ptr)&0x0f]<<' ';
++ptr;
}
You have minor bug here cout << "Byte " [i] which is the same as doing:
const char *text = "Byte ";
cout << text[i]
Another minor bug is that ptr[] is char thus cout treats it as a printable character (not a number), using (int)ptr[i] will do. Also you want print i as decimal and ptr[i] as hex, so you have to use std::dec and std::hex
cout << "Byte " << std::dec << i << " is : " << std::hex << (int)ptr[i] << endl;
You may polish it a bit by using std::setw() and std::setfill() from <iomanip>:
#include <iostream>
#include <iomanip>
using std::cout;
using std::endl;
int main()
{
float number = -1.0;
unsigned char *ptr = ((unsigned char*) &number);
cout << std::setfill('0');
for (int i = 0; i < sizeof(float); i++)
{
cout << "Byte "
<< std::dec << std::setw(0) << i << " is : "
<< std::hex << std::setw(2) << (int)ptr[i] << endl;
}
return 0;
}
This will print out:
Byte 0 is : 00
Byte 1 is : 00
Byte 2 is : 80
Byte 3 is : bf

Why does a nullptr terminated array passed to function through ***char loose the termination element? [duplicate]

This question already has answers here:
Passing a char pointer array to a function
(2 answers)
Closed 8 years ago.
Note 1: I am not looking for different solutions to the problem at hand. I'm curious about what actually is happening here.
Note 2: I'm doing this in c++ context, but am assuming that this also applies to C, hence the C tag. (apart from the representation of a null pointer)
This is about c-strings and access to original from a function. I'll use argv and argc to illustrate that the array should be nullptr terminated. I declare them like this:
int argc = 1;
char **argv = (char**) malloc( (argc + 1) * sizeof(char*) );
argv[0] = (char*)"argument 0";
argv[1] = nullptr;
If I declare a function like this: func1(int &f_argc, char **f_argv) I can access all elements inside the function scope, including f_argv[f_argc], which is nullptr, but I cannot modify the original argv to point to a different address as f_argv in the function is a value passed copy of the original pointer. It has a different address in memory.
If I declare the function like this instead: func2(int &f_argc, char ***f_argv), I can access the original argv through *f_argv in the function, but the last element (which should be nullptr) is cut off. This means that if I try to check for the terminating nullptr inside the function, I try to access an element outside the range of the array, resulting in a core dump at runtime.
Q1: Why is f_argv cut off when reaching the nullptr in func2, but not in func1?
Q2: Is there a way to get write access to the original argv from within the function, without removing the terminator?
Edit: (added code to show what I mean)
#include <iostream>
#include <cstring>
void func1(int &f_argc, char **f_argv) {
using std::cout;
using std::endl;
cout << " In function:" << endl;
cout << " argv passed as **f_argv" << endl;
cout << " f_argv = " << f_argv << " , &f_argv = " << &f_argv << endl;
for (int pos = 0; pos < f_argc; pos++) {
if (f_argv[pos] != nullptr) {
cout << " f_argv[" << pos << "] = \"" << f_argv[pos] << "\"" << endl;
} else {
cout << " f_argv is prematurely terminated" << endl;
}
}
if (f_argv[f_argc] == nullptr) {
cout << " f_argv is correctly terminated" << endl;
} else {
cout << " f_argv[" << f_argc << "] = \"" << f_argv[f_argc] << "\"" << endl;
cout << " f_argv is not terminated" << endl;
}
// Intention is to copy argv here, add elements, terminate it with
// nullptr and change original argv to point to copy. This wouldn't
// work in this function, as &f_argv != &argv.
return;
}
void func2(int &f_argc, char ***f_argv) {
using std::cout;
using std::endl;
cout << " In function:" << endl;
cout << " array passed as ***f_argv" << endl;
cout << " f_argc = " << f_argc
<< " , &f_argc = " << &f_argc << endl;
cout << " *f_argv = " << *f_argv
<< " , f_argv = " << f_argv << endl;
for (int pos = 0; pos < f_argc; pos++) {
cout << " about to check: "
<< "if (*f_argv[" << pos << "] != nullptr)" << endl;
if (*f_argv[pos] != nullptr) {
cout << " *f_argv[" << pos << "] = \""
<< *f_argv[pos] << "\"" << endl;
} else {
cout << " *f_argv is prematurely terminated" << endl;
}
}
if (*f_argv[f_argc] == nullptr) {
cout << " *f_argv is correctly terminated" << endl;
} else {
cout << " *f_argv[" << f_argc << "] = \""
<< *f_argv[f_argc] << "\"" << endl;
cout << " *f_argv is not terminated" << endl;
}
// Intention is to copy argv here, add elements, terminate it with
// nullptr and change original argv to point to copy.
return;
}
// --------------------------------------------
int main() {
using std::cout;
using std::endl;
int argc=1;
char **argv = (char**) malloc( (argc + 1) * sizeof(char*) );
argv[0] = (char*)"argument 0";
argv[1] = nullptr;
cout << "Before function call" << endl;
cout << "argv = " << argv << " , &argv = " << &argv << endl;
for (int i = 0; i < argc; i++) {
if (argv[i] != nullptr) {
cout << "argv[" << i << "] = \"" << argv[i] << "\"" << endl;
} else {
cout << "argv is prematurely terminated" << endl;
}
}
if (argv[argc] == nullptr) {
cout << "argv is correctly terminated" << endl;
} else {
cout << "argv[" << argc << "] = \"" << argv[argc] << "\"" << endl;
cout << "argv is not terminated" << endl;
}
// run one of these
//func1(argc, argv);
func2(argc, &argv);
free(argv);
return 0;
}
If running func2, running the program results in a core dump at this line:
if (*f_argv[f_argc] == nullptr) {
The subscript operator has higher precedence than the dereference operator. *f_argv[f_argc] is *(f_argv[f_argc]). What you want is (*f_argv)[f_argc].
Since you are using C++, you should consider taking f_argv by reference - void f(int &f_argc, char **& f_argv);.

Overloading Class Member Function

I have overloaded a function fn as fn(int,char) & fn(int&,char&) as shown below:
#include <iostream>
using namespace std;
void fn(int a, char c);
void fn(int& a, char& c);
int main()
{
int a=10;
char c= 'c';
cout << "Inside main()" << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
static_cast<void(*) (int&, char&)> (fn)(a, c);
return 0;
}
void fn(int a, char c)
{
int tempInt;
char tempChar;
cout << "\n\nInside Call By Value Function " << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
cout << hex << "&tempInt : " << &tempInt << endl;
cout << hex << "&tempChar : " << (int *)&tempChar << endl;
}
void fn(int& a, char& c)
{
cout << "\n\nInside Call By Reference Function " << endl;
cout << hex << "*a : " << &a << endl;
cout << hex << "*c : " << (int*) &c << endl;
}
The resolution for call to fn(int,char) or fn(int&,char&) is made through cast static_cast<void(*) (int&, char&)> (fn)(a, c);
It gives output:
$ ./Overloading
Inside main()
&a : 0x22ac5c
&c : 0x22ac5b
Inside Call By Reference Function
*a : 0x22ac5c
*c : 0x22ac5b
Now when I put this in a class as below:
#include <iostream>
using namespace std;
class Test{
public:
void fn(int a, char c);
void fn(int& a, char& c);
};
int main()
{
int a=10;
char c= 'c';
Test T();
cout << "Inside main()" << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
static_cast<void(*) (int&, char&)> (T.fn)(a, c);
return 0;
}
void Test::fn(int a, char c)
{
int tempInt;
char tempChar;
cout << "\n\nInside Call By Value Function " << endl;
cout << hex << "&a : " << &a << endl;
cout << hex << "&c : " << (int *)&c << endl;
cout << hex << "&tempInt : " << &tempInt << endl;
cout << hex << "&tempChar : " << (int *)&tempChar << endl;
}
void Test::fn(int& a, char& c)
{
cout << "\n\nInside Call By Reference Function " << endl;
cout << hex << "*a : " << &a << endl;
cout << hex << "*c : " << (int*) &c << endl;
}
I get below error:
$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:42: error: request for member ‘fn’ in ‘T’, which is of non-class type ‘Test()’
How do I resolve this?
How to make a proper call for T's fn(int&,char&)
I guess in my code the expression static_cast<void(*) (int&, char&)> (T.fn)(a, c); is incorrect.
Please help.
Thanks
EDIT:
My mistake
editing Test T() to Test T;
gives error
$ g++ -Wall Overloading.cpp -o Overloading
Overloading.cpp: In function ‘int main()’:
Overloading.cpp:23:44: error: invalid static_cast from type ‘<unresolved overloaded function type>’ to type ‘void (*)(int&, char&)’
First: T is no variable, T is function, that returns Test and receive nothing.
Second: function-pointer is not member-function-pointer. You should use this syntax
typedef void (Test::*function)(int&, char&);
function f = &Test::fn;
(T.*f)(a, c);
See error: request for member '..' in '..' which is of non-class type: the problem is with Test T();, try ommiting the parentheses.
When you do Test T();, you're saying that T is a function with return type Test. However no such thing exists in your code.
The solution is:
Test T;