Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 days ago.
Improve this question
int* p= NULL;
int** pp = &p;
cout << &p << endl;
cout <<*pp << endl;
I think the output results of the two should be consistent, but the first output is the address of p, and the second is 00000000. I want to know why.
You are mistaken, pp is the address of p
int** pp = &p; // set pp to the address of p
But *pp is the value of whatever pp is pointing at. In this case that is p which has a value of NULL.
The code is essentially no different to this
int x = 123;
int* px = &x;
cout << &x << endl; // print address of x
cout << *px << endl; // print value of x i.e. 123
The only difference being the type of the original value (int vs int*).
pp holds the address of p, so print pp, not *pp.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I am attempting to get random numbers out of the getrandom() function however attempting to use it only returns -1 the code i am using below:
#include<iostream>
#include <sys/random.h>
int main(){
void* d = NULL;
ssize_t size = 10;
ssize_t p = getrandom(d, size, GRND_RANDOM);
std::cout << p << std::endl;
}
getrandom returns the number of bytes written. The first argument is the pointer to a byte buffer (to be filled with random bytes), the second argument is the number of random bytes that you want to be written to the buffer.
Your return value (p) being -1 means that there was an error when writing the random bytes to the buffer. This error in your case is because you are passing in NULL as the pointer to the buffer to be filled.
Try this instead:
#include<iostream>
#include <sys/random.h>
int main(){
unsigned char random_bytes[2]; // buffer where getrandom will store the random bytes
ssize_t size = 2;
ssize_t p = getrandom((void*) &random_bytes[0], size, GRND_RANDOM);
std::cout << "First random value: " << random_bytes[0] << std::endl;
std::cout << "Second random value: " << random_bytes[1] << std::endl;
}
source:
https://man7.org/linux/man-pages/man2/getrandom.2.html
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 2 years ago.
Improve this question
Let me preface this by saying I am a c++ rookie, completely new and learning in school right now. One question I am stumped on is dynamically created memory and how to point back to it. For the assignment I have, I created the memory, printed in the console, then my task is to point back to dynamically created memory that I made at the beginning of the assignment. How do I do that? I must have to create a temporary variable or reference variable of some sort, but I'm just unsure of how to.
// Task 1
//int *p;
int j = 18;
int *p = new int(2);
cout << "Dynamically created integer: " << *p << endl;
*p = j;
cout << "Named integer: " << *p << endl;
cout << "Return to dynamically created integer: " << *p << endl;
delete p;
That second to last line where I have the *p, that is incorrect, I just need to point back to the 3rd line of code somehow. Thank you!
You have a slight misunderstanding of how pointers work. We'll correct it here.
*p = j;
You think that this changes p to point to j. It does not. But let's proceed.
cout << "Named integer: " << *p << endl;
You are now thinking that this shows what p is pointing to now. And on the next line you want p to point to what it was, before.
Except that p is always pointing to the same thing, all the time. This line:
*p = j;
This does not point p to j. This sets the value that p points to, to the value of j. This is a very subtle difference. To change p to point to j, the correct syntax would be:
p = &j;
However, at this point, it becomes logically impossible to restore the original pointer. You have to save it first:
int *old_p=p;
p = &j;
Then, after you print what's p is pointing to now, you can simply restore it:
p=old_p;
And now the second print statement will show the original value that p was pointing to.
From my understanding, you're trying to get the memory location of j, for that you can use the "address of" ( & ) operator such as 'std::cout >> "memory address of j = " >> &j >> '\n';'
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I've been reviewing my C++ lately. But I am running into a puzzle about printing a char array. The code is below:
int n = 5;
char *array1 = new char[n];
for (unsigned int i = 0; i < n - 1; i++)
array1[i] = (char)i;
cout << array1 << endl;
cout << array1[3] << endl;
cout << *array1 << endl;
None of the three cout lines works. Could anyone tell me why?
array1[0] == 0. cout << array1 interprets array1 as a pointer to a NUL-terminated string, and since the very first character is in fact NUL, the string is empty.
cout << array1[3] does print a character with ASCII code 3. It's a non-printable character, not visible to a naked eye. Not sure what output you expected to see there.
As a separate answer, it seems you're trying to get a string which has the following : array = "1234....(n-1)"
Try :
for (int i = 0; i<(n-1); i++)
array1[i] = (char)i - '0';
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
using namespace std;
int main()
{
int n, *p1, *p2;
n = 10;
p1 = &n;
p2 = p1;
(*p1)++;
(*p2)++;
cout << *p1 << " " << *p2 << " "<< n << endl ;
return 0;
}
*p1 refers to the value pointed by the pointer p1. (*p1)++ will increment the value of n by 1 and (*p2)++ will again do the increment on n, since it is pointing to the same location of p1. So the n will be incremented to 12. *p1,*p2 and n will thus have 12. So it prints 12.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I basically have a bool get function which returns true or false and sets the values.
This is my method...
bool Student::getMarks(int i, double *a, double *b)
{
{BOUNDS CHECKNG DONE HERE...
return false;}
THE LINE BELOW WORKS FINE AND PRINTS TO TERMINAL
//cout << marks[i][0] << "," << marks[i][1] << endl;
//THIS BIT CRASHES THE PROGRAM
*a = marks[i][0];
*b = marks[i][1];
return true;
}
This is my test program...
Test.cpp
double *a;
double *b;
Student student;
case 1:
student.getMarks(i, * a, *b);
cout << *a << endl;
cout << *b << endl;
break;
default:
break;
If a mark exists it returns true and sets the mark to *a and *b.
However, the program crashes.
Would greatly appreciate it if someone could tell me where I am going wrong. I want it so that the test program actually returns both *a and *b
You want this:
double a;
double b;
Student student;
case 1:
student.getMarks(i, &a, &b);
cout << a << endl;
cout << b << endl;
break;