C++ print value of a pointer - c++

I have an array of double pointers, but every time I try do print one of the values the address gets printed. How do I print the actual value?
cout << arr[i] ? cout << &arr[i] ? they both print the address
Does anyone know?

If it's really an array of (initialized) double pointers, i.e.:
double *arr[] = ...
// Initialize individual values
all you need is:
cout << *arr[i];

cout << *(arr[i]) will print the value.

cout << *(arr[i]);

If "arr" is declared as
double* arr[..];
Then you would use:
cout << *(arr[i])

Related

C++: Accessing the values of an "array pointer" without the dereferencing operator?

Apologies if this question was already asked, but there are too many questions with C++ arrays and pointers as that I could go through all of them..
Let's say we declare a double array like this:
double val[3] = {-3.1415, 6.2430, +37};
// Declare and initialize pointer variable:
double * pointer = &val[0];
cout << "Elements of the array are: " << endl;
cout << pointer[0] << " " << pointer[1] << " " << pointer[2] << endl;
What confused me is how I get as output the correct values stored in the double array val, without having to use dereferencing operator *, so for example *pointer[0]..
pointer points at the first double in an array of packed doubles.
pointer[1] means *(pointer+1) in a ridiculously fundamental way, so much so that 1[pointer] works. (don't do that btw)
If you have an array of data, then adding 1 to a pointer to the first element gets a pointer to the second.

Get value of twodimensinal dynamic array from storred pointer adress

Im trying to get all values of a dynamic array initialised like this:
string** structure= new string*[nombre_attributs]();
for(int j=0; j<nombre_attributs; j++){
structure[j]= new string[2]();
}
I can fill it with no problem. I then keep a pointer to this array in an array of pointers called adresses_structures.
How can I now acces the data in this array?
This attempt:
string *test = adresses_structures[i];
cout << "Value:" << *(test+0)<<endl;
cout << "Value:" << *(test+1)<<endl;
cout << "Value:" << *(test+2)<<endl;
doesn't work for all values from the second line in the array.
I have tried tons of combinations as
for(int k=0; k<2;k++){
for(int j=0;j<2;j++){
cout << "Truc:" << *(test + k*2 + j)<<endl;
}
}
but nothing seem to be able to get the values on the second line.
Thanks for any advice.
I assume, that string** structure is the same as adresses_structures here.
When You are doing *(test+1) You move by one byte from test address. You should move by size of string object (check sizeof(string)). Like this: *(test+1*sizeof(string)) or *(test+2*sizeof(string))

Printing out the value of pointer to the first index of an char array

I'm new to C++ and is trying to learn the concept of pointer. When I tried to print out the value of pStart, I was expecting its value to be the address of text[0] in hexdecimal (e.g. something like 0x7fff509c5a88). However, the actual value printed out is abcdef.
Could someone explain it to me why this is the case? What parts am I missing?
char text[] = "abcdef";
char *pStart = &text[0];
cout << "value of pStart: " << pStart << endl;
Iostreams provide an overload that assumes a pointer to char points to a NUL-terminated (C-style) string, and prints out the string it points to.
To get the address itself to print out, cast it to a pointer to void instead:
cout << "value of pStsart: " << (void *)pStart << "\n";
Note that you don't really need pStart here at all though. The name of an array (usually, including this case) evaluates to the address of the beginning of the array, so you can just print it directly:
cout << "address of text: " << (void *)text << "\n";
Get out of the habit of using endl as well. It does things you almost certainly don't realize and almost never want.

Pointers & converting a char array to an int

I am doing some exercises to figure out how to access values in an array after they are changed with pointers. Can someone point out why the first output does not show the desired output? I am trying to get both cout to print 1234, one by using the new pointer and one by using the position in the array
int main()
{
char myArray[50]={0};
short* sizeOfAlloc=(short*)(myArray+5);
*sizeOfAlloc=1234;
cout << (short*)(myArray+5) <<endl;
cout << *sizeOfAlloc <<endl;
system("pause");
}
cout << (short*)(myArray+5) <<endl;
Prints the pointer. Not the value pointed by it.
cout << *((short*)(myArray+5)) <<endl;
^^ ^^
Will print the value pointed to by (short*)(myArray+5)

Referencing elements from two-dimensional array with a single value

I am probably missing something fundamental, but i cannot find a solution to the following issue.
I have a two-dimensional array of some float elements and i am trying to find a way to be able to reference them by using only a single value.
Example:
float test[5][50];
test[3][25] = 34.67;
cout << test[3][25] << endl;
int id = 25;
cout << *test[id*5+3] << endl;
I am hoping to get same result from both cout. Instead my output looks like this:
34.67
Segmentation fault
What am I doing wrong?
Thanks!
Without testing, I think something like this might work. Note that C++ arrays are major->minor from left dimension to right dimension.
float test[5][50];
test[3][25] = 34.67;
cout << test[3][25] << endl;
int id = 25;
float* test2 = &test[0][0]
cout << test2[(3 * 50) + id] << endl;
test is a float[][] with 5 elements (each of which is a float[] with 50 elements), and you are referring to test[128]. Hence the seg fault.You need to convert from single index to subscript using integer division and mod:
cout << test[id/50][id%50] << endl;
You should assert(id/50<5); to make sure your index is within bounds.
The crash is because you are trying to read the contents of the element as a memory address
*test[id*5+3] means int address = test[id*5+3]; then read memory at address.
If that address is 0 or memory you don't own then it will crash.
Accessing a two-dimensional bitmap (image) with a single reference is often useful.
Use:
int image2D[dim2][dim1];
int *image1D = &image2D[0][0];
Now
image2D[i][j] == image1d[i*dim1+j]
I agree with the first comment, that you really shouldn't do it, but I'll answer anyway.
I think that if you try:
cout << test[id*5+3] << endl;
it should work. There is no need to dereference with *.