Getting different results when dereferencing a pointer to an integer [duplicate] - c++

This question already has answers here:
How to increment a pointer address and pointer's value?
(5 answers)
Closed 3 years ago.
#include<stdlib.h>
#include<iostream>
using namespace std;
void fun(int* a){
int b=*a++;
cout<<b<<endl;
}
void fun1(int *a){
int b=*a+1;
cout<<b<<endl;
}
int main(){
int n=5;
fun(&n);//output remains 5
fun1(&n);//output incremented by 1
}
In the function fun the value of n does not get incremented when done as shown in the above code, on the other hand in function fun1 the value of n gets incremented by 1.What is the problem with the first approach to increment n?

The behavior you observe is expected by the operator precendence rules.
In the first case it is a++, the previous value of a dereferenced.
In the second: the value of a dereferenced plus 1.

Related

why the code is printing an abnormal value [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
What (actually) happens, when a function with the warning "control reaches end of non-void function" is called?
(7 answers)
Closed 4 days ago.
the code is printing abnormal value `// C++ Program to demonstrate working of a function
#include <iostream>
using namespace std;
int max(int x, int y)
{
if (x > y)
cout<<x;
else
cout<<y;
}
// main function that doesn't receive any parameter and
// returns integer
int main(){
int a = 10, b = 20;
// Calling above function to find max of 'a' and 'b'
cout<<max(a, b);
return 0;
}
i am expecting the answer to be 20 can explain why this is happening

Why int b; has 16 value in shell/terminal? [duplicate]

This question already has answers here:
What happens when I print an uninitialized variable in C++? [duplicate]
(4 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cout<<a<<endl;
cout<<b;
}
Why b has 16 value when i compile this? I cant understand why b has 16 not 0. Int doesnt have 0 as default value?
Absolutely not. C++ does not initialise variables unless you ask it to. (Setting a variable to 0 for example is at least one instruction: typically reg XOR reg. That could be wasteful.) The behaviour of reading an uninitialised int is undefined.
(Note you can do some things with uninitialised variables, such as setting a pointer or reference to one, computing sizeof(a), and using decltype(a). But passing it by value to a function is undefined behaviour. That often trips up even professional programmers.)

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

I don't understand how value is changing between next and prev in the program? [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 6 years ago.
#include<bits/stdc++.h>
using namespace std;
int main()
{
int prev=-1;
int next=1;
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<prev+next<<endl;
next=prev+next-next+(prev=next);//assigning prev and next
}
}
I can not understand assigning prev and next.In second line between for loop how operator precedence works?
First of all, never #include<bits/stdc++.h>. Include headers you need as per specification.
Second, your example has undefined behavior in it - you are modifying and reading the same value before reaching sequence point.

Why is the size of array not same in both the cases? [duplicate]

This question already has answers here:
Why is sizeof(array) different in these two cases? [duplicate]
(2 answers)
Closed 7 years ago.
#include<iostream>
using namespace std;
void f(int arr[])
{
int a=sizeof(arr);
cout<<a;
}
int main()
{
int n;
cin>>n;
int arr[n];
int a=sizeof(arr);
cout<<a<<"\n";
f(arr);
}
Output:
24
8
Why is my output not same in both the cases even when i am printing the size of same array?
In the first call it shows the size of array.
But in the second case it shows the size of the pointer passed.
When arrays are passed through functions they decay into pointers. So after passing, the:
sizeof(array)
Shows the size of the pointer.