Pointers & converting a char array to an int - c++

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)

Related

Converting an integer to Char Pointer using C

I am trying to convert an integer to a char pointer as shown below. The data results are different. I am not sure what is going wrong. Please help me in correcting the code.
int main(){
char *key1 = "/introduction";
std::ostringstream str1;
str1<< 10;
std::string data=str1.str();
std::cout <<"The data value="<<data<<std::endl; // The data value= 10
char *intro= new char[data.length()+1];
strcpy(intro, data.c_str());
std::cout <<"The data value="<<*intro <<std::endl; // The data value=1
return 0;
}
I am not sure why two data value are printed different i.e, 10 and 1.
In C++, when trying to print all the contents of a char * with cout, you should pass the pointer, i.e. cout << intro << endl.
What you've done here is dereferenced the char *, so cout << *intro << endl is equivalent to cout << intro[0] << endl, which is equivalent to printing the first character, 1.

Pointer printing first char of string

#include <iostream>
using namespace std;
char* input(char **S);
int main()
{
char **S = new char *;
char *K = input(S);
//cout << K << endl;
cout << *K << endl;
}
char* input(char **S)
{
cout << "Enter string: ";
cin >> *S;
cout << S << endl; // Prints address of S
cout << *S << endl; //Prints content of address stored in S
return *S;
}
I am failing to understand why when I print out *K, I just get the first character of the input string but if I print out the commented line(just K alone) I get the whole string. Any help with explaining what I am not able to see or understand is appreciated.
Let's understand how arrays work:
// Let's say I have one character array
char arr[] = {'a', 'b', 'c', 'd'};
In here, the name of the array i.e. arr acts as the pointer to the first element of the array. However, do note that it is NOT the pointer to the first element to avoid confusion, it just have an implicit conversion to pointer of element type. More details can be found here: https://stackoverflow.com/a/1641963/10821123
Now since array is contiguous, the rest of the elements can be determined.
// so ideally, the below two statements would print the same thing
cout << &arr << endl;
cout << (void*) &arr[0] << endl;
// the above line just takes out the address of the first pointer
Now coming to your question, I'll convert my example to a string one:
char *K = "abc";
cout << *K << endl; // a
cout << K << endl; // abc
Note that the above assignment of char *K = "abc"; will give you a warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
The pointer only holds the address of the first element of the array, so when you dereference the pointer, it prints the first element, i.e. *K is interpreted as K[0]
Now there's an overload of operator <<, so what it does is if it sees a character pointer i.e. char*, it prints the complete null-terminated string, that's why in your case too, it is printing the whole string.

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

Char* pointers and char[] [duplicate]

This question already has answers here:
What is array to pointer decay?
(11 answers)
Closed 7 years ago.
I'm working on learning pointers in C++, and was doing fine with int*, double*, etc. but then I tried something that I can't understand. I've attached my code segment, and the comments are the terminal output.
char word[] = "Hello there";
cout << word << endl; //Hello there
cout << *word << endl; //H
cout << &word << endl; //Address
char *wordpt = word;
cout << wordpt << endl; //Hello there
cout << *wordpt << endl; //H
cout << &wordpt << endl; //Address
wordpt = &word[0];
cout << wordpt << endl; //Hello there
cout << *wordpt << endl; //H
cout << &wordpt << endl; //Address
What's going on in these? I don't even understand how the contents of word (*word) can be a single index. How is word stored in memory? And why would wordpt allow me to give it a value of word, which isn't an address?
The pointer wordpt in the first case points to the first element of the array word, the second assignment is exactly the same thing but explicitly.
Arrays are automatically converted to pointers to their first elements as is specified by the c standard which also applies to c++.
What is confusing you is the fact that cout automatically prints the contents of a char * pointer instead of the address the pointer points to, to do it there is a requirement, the pointed to data must be a c string, i.e. a sequence of non-nul bytes followed by a nul byte, which is what is stored in word.
So cout is accessing the data pointed to by the wordpt pointer.

C++ print value of a pointer

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])