This question already has answers here:
Adding an offset to a pointer
(3 answers)
Closed 5 years ago.
In the cplusplus page for the inner_product module they gave a code example:
int init = 100;
int series1[] = {10,20,30};
int series2[] = {1,2,3};
std::cout << "using default inner_product: ";
std::cout << std::inner_product(series1,series1+3,series2,init);
std::cout << '\n';
Where I saw them use series1+3 when calling the inner_product funtcion.
What exactly does adding "3" to the array variable do?
What exactly does adding "3" to the array variable do?
The array operand decays into a pointer to first element of the array. 3 is added to that pointer, so that the result is a pointer to 3 elements after the first (i.e. the element at index 3), which is immediately outside the bounds of the array. The addition is equivalent to std::end(series1) which would be more idiomatic in my opinion.
I'm assuming that series1 on itself is a pointer to the first element of the array
You assume wrong. series1 is not a pointer, but an array. However, an id-expression (standardese for the name of the variable) of an array will decay into a pointer in value contexts.
Related
This question already has answers here:
How do I find the length of an array?
(30 answers)
Closed 3 years ago.
I was just messing around playing with some code then i suddenly see that after passing a array by reference sizeof(arr)/sizeof(arr[0]) is giving me other output than i expected.If i am just passing the reference then the len=1 may be justified since sizeof(arr[0])/sizeof(arr[0]).What is wrong with this any concept i am missing?
#include<iostream>
using namespace std;
void display(int *arr)
{
int len=sizeof(arr)/sizeof(arr[0]);
cout<<len<<"\n";
for(int i=0;i<50;++i)
{
cout<<arr[i];
}
cout<<"\n";
}
int main()
{
int arr[50]={0};
for(int i=0;i<50;++i)
{
arr[i]=i;
}
display(arr);
return 0;
}
Why is output of lenth not 50 as it should be?Why is it only 2 instead of 50.
Whenever you try to pass an array to any function. The array is implicitly converted to pointer to first element of array. Here in above code as you passed integer array it get converted to
integer pointer which is pointing an array.
That's why you are getting such result. In array cases you need to pass size explicitly in the function. So sizeof is working fine , it is dividing
size of "pointer to array" with the "size of one element".
Reason behind such casting is that "As you must know that arguments are copied into parameters of the function. But in case of arrays.Array can be of variable size they may be larger size. So it's unnecessary to copy such large array.That's why language implement this casting automatically. "
This question already has answers here:
Accessing an array out of bounds gives no error, why?
(18 answers)
Closed 4 years ago.
int main()
{
int n[] = {2};
cout <<n[1] << endl;
return 0;
}
In this code, I get some numbers,also for n[2],n[3] etc. I got ë for a string array and # for a char array. I don't think it is the memory location so what is this?
int n[] = {2};
Here you are declaring an array with one element. So now n[0] is 2. That's the only element, so everything else is out of bounds. Therefore this here:
cout <<n[1] << endl;
Is undefined behavior, because you're accessing the second element, which doesn't exist. Remember, array indexing starts at 0, so element 0 is the first one and 1 is the second one.
So why does it print a rubbish value, then? Since it's undefined behavior, there's no guarantee for what it does, but what's most likely going to happen is that it's calculating the respective memory location and prints the data at the resulting address, interpreted as int. That usually results in some rubbish value.
This question already has answers here:
Why a pointer + 1 add 4 actually
(6 answers)
Closed 5 years ago.
I have been learning about using pointers with arrays. If I was to write this simple code:
string Array[3] = {"one", "two", "three"};
string *pArray = Array;
pArray = pArray + 1;
std::cout << *pArray << std::endl;
The output is 'two'
Can someone explain to me the workings of this? Why does the +1 change the position of the pointer to where "two" is in the array? I'd expect the +1 to be concatenated onto the end of the string pointer so I'd end up with something like '0x61feb01'.
How does the compiler know to increment the array and not just add a 1 on to the end of the pointer string memory location?
Why is adding an int to a string different here?
Thanks.
I think when you add "1" to your pointer, this "1" is not treated as an integer number in the process of addition. Notice that pointer is an address, now, when you add 1 to your pointer it becomes: the address it currently is+ONE COMPLETE SIZE of a string type. So for example if your string type takes 8 Bytes you're moving 8 Bytes forward in the memory and not 1 Bytes!
As to why "two" printed you should know that array elements are held in continuous groups of bytes in the memory thus when you add to the address of one of the elements you can get to the other elements. Array names are nothing but pointers themselves except that they are fixed pointers(you can't change their addresses). E.g. In (int myArr[10]) the name "myArr" is a pointer that points to the(has the address of the) first part in the ram that holds the first element and then using pointer arithmetic just as you've done in the above example of yours you can access the rest of the elements either.
As an ending note, these two are equal in this example array: (myArr[i]==*(myArr+i)), if you put 0 here instead of i, you get (myArr[0]==*myArr) which is exactly what I said earlier. I hope it helps.
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 7 years ago.
I saw the following line of code in C++. I have trouble understanding it. I hope that I can get some help here.
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a[] = {1,2,3,4};
cout<<*a+1<<;
cout<<a[1];
}
Question:
I don't understand how *a+1 works. It seems pretty unintuitive - are we adding 1 to the array here?
To understand the expression *a+1 you should consider the operator priority:
the dereference * has higher priority, so a (the name of an array is also the pointer to the first element) is dereferenced, giving the value 1
Then, 1 will be added to the value.
operator priorities are describe here http://en.cppreference.com/w/cpp/language/operator_precedence
In the expression *a + 1, the array a is evaluated in pointer context, so it points to the first value in the array.
Next, *a dereferences that pointer (meaning it gets the value the pointer points to), and that gives you the first element in the array which is 1.
Finally, 1 is added to that value and printed, so it outputs 2.
First of all this statement
cout<<*a+1<<;
^^^^
is syntactically wrong.
I think you mean something like
cout << *a+1 << endl;
Expression *a is equivalent to a[0]. So you could write instead
cout << a[0] + 1 << endl;
Take into account that semantically a[0] + 1 is not the same as a[1] though applied to your example the both expressions will yield the same result.:)
This question already has answers here:
Strange output when printing the value 0x89 (-119)
(3 answers)
Closed 8 years ago.
#include<stdio.h>
int main()
{
unsigned int a=0xabcdef12;
char *c=(char *)&a;
int i;
for(i=0;i<4;i++)
{
printf("%x.....%x\n",*c,c);
c++;
}
return 0;
}
O/p:
12.....bfad5bd4
ffffffef.....bfad5bd5
ffffffcd.....bfad5bd6
ffffffab.....bfad5bd7
If you see during first print, it is printing 12 but in all the subsequent prints it is printing correct values but padded with ffffff. Can someone explain this difference??
You are messing with pointers. Be careful out there. The line
char *bit_longer_name=(char *)&a;
says that "gimme pointer to CHAR type data array, and call it 'bit_longer_name'. The array initializes at variable 'a' address". Now when you do
bit_longer_name++
it actually travels that pointer forward in memory -- goes to next element in a array -- like my_char_array[i] does. The print you have there:
printf("%x.....%x\n",*c,c);
The first part prints the "value of the current array cell i am pointing to", and you are actually passing invalid value for the printf (you should get warning from that!), that causes the compiler to read over from the memory spot pointed by 'c' (-> its working in the first place).
The second part on the other hand is the "address i am pointing to with 'c'", and it travels forward as you can see.