in post increment Lvalue error [duplicate] - c++

This question already has answers here:
pointer increment and dereference (lvalue required error)
(4 answers)
Closed 9 years ago.
int main()
{
int[] x={1,2,3,4,5};
printf("%d",x);
printf("%d",*x);
printf("%d",++*x);
printf("%d",*x++); //Here Lvalue required erroe is genrerated
}
Can someone please explain me what is meaning of this error and why it is generating here

You can't increment an array.
*x++ is the same as *(x++). Maybe you wanted (*x)++ instead?

The correct way to declare array in c/c++ is :
int x[]={1,2,3,4,5};
The error is because you can not use ++ on array

In this statement:
printf("%d",*x++);
You are applying the ++ operator to the array itself, which is not possible. This gives you the lValue error.
Alternatively, in this one:
printf("%d",++*x);
You are applying the ++ operator to the value pointed to by the array (its first element), which of course works fine and just prints the value of next element.
Try
printf("%d",(*x)++);

You can't increment an array. Try this:
int main()
{
int x[]={1,2,3,4,5};
printf("%d",x);
printf("%d",*x);
printf("%d",++*x);
int *y=x;
printf("%d",*y++); //you can increment a pointer though
}

its ok the increment in array makes no sense.But array is just a pointer variable. I mean if I print the value of x in my program(which is an array) it will print address of a[0].It mean it is pointer to a[0].So when I can use increment on a simple pointer variable then why I can not use increment operation on array pointer.

Related

C++ returned array loses it's values depending on if any new array is declared or not. Its losing it if yes [duplicate]

This question already has answers here:
Returning an array using C
(8 answers)
Closed 6 months ago.
I wrote a simple c++ program where i am getting a simple float Array from a function. Then I call a function where the generated Array is a parameter of. I print the first value of the Array through the function. It works! BUT when i am declaring another array in the function before i print the value from the first array, the printed value is not the expected one. MAGIC!
CODE THAT DOESN'T WORK:
#include <iostream>;
float* getArray() {
float Array[] = { 4 };
return Array;
}
void compute(float* Array) {
bool newArray[1];
float content = Array[0]; //breakpoint: content will be -107374176.
std::cout << content;
}
int main() {
float* Array = getArray();
compute(Array);
}
OUTPUT: -1.07374e+08
When removing the line "bool newArray[1];" (line 10) the code starts to work.
OUTPUT: 4
Test it your self! Can someone explain me why this happens?
Your main function has a bug. It passes a pointer to Array to compute, which dereferences it. But Array is out of scope at this point since its scope ends when getArray ends. So you are trying to access an object that no longer exists. That is undefined behavior and produces completely unpredictable results.
Arguably, getArray also has a bug since it always returns a pointer to an object that no longer exists. So there is no way to use it. Whether or not that's a bug depends on what the caller is supposed to be able to do with the returned value which cannot be determined due to the lack of comments.

Why is sizeof Function not working here as it is supposed to [duplicate]

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. "

Adding a number to an array variable in c++ [duplicate]

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.

Pointer of array in C++ [duplicate]

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

Indexing an int? How does this work? [duplicate]

This question already has answers here:
Why does i[arr] work as well as arr[i] in C with larger data types?
(5 answers)
Closed 7 years ago.
The following code
#include<stdio.h>
int main()
{
int arr[] = {10,20,30};
cout << -2[arr];
return 0;
}
prints -30. How? Why?
In your case,
cout<<-2[arr];
gets translated as
cout<<-(arr[2]);
because,
array indexing boils down to pointer arithmatic, so, the position of array name and the index value can be interchanged in notation.
The linked answer is in C, but valid for C++ also.
regarding the explicit ()s, you can check about the operator precedence here.
Look at this statement
cout << -2[arr];
First, know that even though it looks strange the following is true
2[arr] == arr[2]
That being said operator[] has higher precedence than -. So you are actually trying to invoke
-(arr[2])
In C and C++, 2[arr] is actually the same thing as arr[2].
Due to operator precedence, -2[arr] as parsed as -(2[arr]). This means that the entire expression evaluates to negating the 3rd element of arr, i.e. -30.