Casting to char pointer [closed] - c++

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
Let's take the following code for example
int number = 1;
char * charsequence = (char *)&number //casting the address of number to char *
std::cout << charsequence << endl;
The above snippet produces the following result:
☺
If I change the number then a different symbol appears.
That's fine, the real question is why do I always get the same symbols even though the memory location (&number) is different on each run? And the most important part is why do I get symbols instead of my memory address?
I assume that the casting I did is not working as I thought it was.
Edit: Since it's closed as unclear of what I'm asking, here's there real question:
How do i print the memory address of an object to the console?
At the time of this edit, this question was already answered. See the accepted answer

std::ostream has a special overload for operator<< which, when you give it a char *, will not print the value of the pointer, but instead assume that you gave it a pointer to the first element in an array of characters that is null-terminated, and then try to print the entire array up to the terminator. (This overload allows you to print C strings in a sort-of natural looking syntax.)
Since your pointer does not in fact point to the first element of a null-terminated array, your program has undefined behaviour.
If you want to print the pointer value, you should use a void pointer:
std::cout << static_cast<void*>(&number) << "\n";

Related

Declaring a char pointer [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 10 months ago.
Improve this question
I'm trying to make a login/register project and I have difficulties in declaring the char* tempUsername from this code (SIGSEVG segmentation fault)
char *tempUsername, *tempPassword, *tempPasswordConfirm, *tempSecurityQuestion;
/*
no other declaration for tempUsername here
*/
std::cout<<"Enter your new username:\n";
std::cin>>tempUsername;
//process stops here
if(fileSearch(newFilename(tempUsername))) {
std::cout<<"Username already exists! Choose another username!\n";
}
else {
std::cout<<"Enter your password:\n";
std::cin>>tempPassword;
std::cout<<"Confirm your password:\n";
I'm having a hard time understanding anything about pointers, so any advice is more than helpful!
char *tempUsername
std::cin>>tempUsername;
The problem here is that your pointer is uninitialised. When you try to extract from the input stream into the uninitialised pointer, the behaviour of the program will be undefined. Don't do this.
Your goal seems to be to read a string of user input. A solution that I can recommend is to use the std::string class:
std::string tempUsername;
std::cin >> tempUsername;
No need to use a pointer to achieve this.
I can use a char* as an array of chars, is that true?
It is not true in general. If you have a char* that points to an element of an array of char, then you can use that char* as an iterator to access the elements of the array.

how to find the address of a function in a c++ program [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 3 years ago.
Improve this question
i need to find the address of func in the stack so that if i provided a 32 long "A" string and then the function address in the stack i would get the Access granted
i remember i did it using objdump but i can't seem to figure it out
#include <iostream>
using namespace std;
void func()
{
cout << "Access Granted \n";
}
int main()
{
char buff[20];
cin >> buff;
cout << buff;
return 0;
}
i tried immunity debugger but i was not successful
how to find the address of a function in a c++ program
You can use the addressof operator to get a function pointer to the function. This pointer stores the address. Note that this cannot be done for non-static member functions because you get a member function pointer rather than a function pointer.
To print the address, you can convert the function pointer to void*. Note however that not all systems support this conversion.

What is different between while(aPointer) and a if(aPointer) in C++? [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 3 years ago.
Improve this question
I'm working on a print function and my original codes is like
void print1(const char *cp)
{
while(cp)
{
if(*cp)
{
cout << *cp++ << " ";
}
}
}
it would never stop until I changed it to
void print1(const char *cp)
{
if(cp)
{
while(*cp)
{
cout << *cp++ << " ";
}
}
}
I got a little confused about this code, actually it's a code in C++Primer. Did while and if consider the same thing? But why the first one cannot stop? Is that because there is a pointer points to the last location but it has nothing in it, the while would be true forever but will never get into the if?
In the first example, the loop runs until the pointer itself becomes null, which it never does (instead, it's eventually incremented past the end of the buffer, whereupon the program exhibits undefined behavior).
In the second example, the loop runs until the character pointed to becomes zero - which it does once the advancing pointer reaches the end of a nul-terminated string.

What is "int * a[][10];"? [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 6 years ago.
Improve this question
The question is in the title:
What is int * a[][10];
Is it an array of pointers to arrays of int? I tried to youse the clockwise/spiral rule but I am not sure...
int * a[][10];
is an illegal declaration in C++, as the storage size isn't known. You need to initialize it with arrays of 10 pointers to int, like so:
int* a[][10] = {{nullptr}}; // initialize with one array, the latter consisting of null pointers
or, even simpler,
int* a[][10] = {{}};
Once initialized, it becomes an array of arrays-10 of pointers to int.
int *a[][10], is to be read as pointer to 2D array of undefined rows (by default I guess it is 0) and 10 columns,with data read out row-wise in memory layout.
But this declaration is only possible in C.
If we declare a 2D array namely int abc[10][10] then we can make 'a' to point this array 'abc'.

c++ 2D char array element initializing with const char [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 8 years ago.
Improve this question
I am trying initialize element of array
array[m][n] == char("X");
after printing that element i'm getting the value of it equals д (Russian d); how deal with it, and I'm not even able to initialize that element without parsing const char to char.
You have to write simply as
array[m][n] = 'X';
where 'X' is a character literal.
Or if you like very much string literals then:)
array[m][n] = *"X";
or
array[m][n] = "X"[0];
EDIT: I am sorry. You have also to use the assignmnet operator (=) instead of the comparison operator (==)