Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
While printing the address and value of 'X' in function foo1 and the address and value of Y in foo2, why it is showing the same values for both of the functions?
#include <stdio.h
void foo1(int xval)
{
int x;
x = xval;
/* print the address and value of x here */
}
void foo2(int dummy)
{
int y;
/* print the address and value of y here */
}
int main()
{
foo1(7);
foo2(11);
return 0;
}
Output of the program is
Address of X is: 65518
Value of X is: 7
Address of Y is: 65518
Value of Y is: 7
It's because they're created on the stack, which is unwound after each function call. So they are created at the same memory address.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
struct Point
{
double x,y;
};
Point p;
struct Disk
{
Point center;
int radius;
};
Disk d;
int main()
{
d.center.x=1.2;
cout<<p.x;
}
Could someone please explain me the output of this code?
why am I not getting the value of x as 1.2 and 0 instead?
Let's go through your code line by line.
First, you created a Point called p. So p is sitting in the memory somewhere:
Memory: p:[x, y]
Then, you created a Disk called d, which stores it's own Point object inside it.
Memory: p:[x, y] d:[center:[x, y], radius]
These are completely separate objects. When you modify the Point stored in d with d.center.x=1.2, it does not affect p at all.
Therefore, p is uninitialized, and reading the value of uninitialized variables causes undefined behavior, meaning anything can happen, in this case usually getting a random value.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
what makes type cast affected this?
where is the value stored?
The program has undefined behavior: with the const_cast<int*>(b) you remove the const qualifier from an object which actually is const and the assignment to that object may have arbitrary effect.
The observed effects indicate that the implementation replaced uses of a with its immutable value while it dereferences b to determine the value. It could have arbitrary other effect, too, though. For example, a segmentation fault when trying to write a write protected location could be a possible outcome. Well, anything can happen.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Is it possible to assign value of one structure variable to another structure variable
Is this code correct -
#include<iostream.h>
struct s1
{
int a;
float b;
char c;
} st1,st2,st3;
int main()
{
struct s2{
int x;
float y;
char z;
} ss1,ss2,ss3;
// Read & Initialize structures
ss2=ss1;
:
ss3.z=st1.c;
:
}
void func1()
{
ss2.x=st1.a;
ss3.y=st2.b;
ss1.z=st3.c;
:
ss1=ss3;
}
Kindly clear my doubt whether the above code is OK or not
ss2=ss1; this will depend on the compiler some compiler will allow to copy the structure variable and some not.
ss2.x=st1.a; ss2 structure will be local to the main function and it is not available in funct1(), so it must be throwing the compilation error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
constexpr size_t b = 10;
int arr[b];
for ( int i = 0; i<b; i++)
{
arr[i] = i;
}
for ( int x : b)
{
cout<<x;
}
}
The code is displayed above.
Why can't I print the contents of an array using the range for loop? When I try to, it gives me an error saying
error: no matching function for call to 'begin(const unsigned int&)'|
Why can't I print the contents of an array using the range for loop?
You can; but the range is the array arr, not its size b:
for (int x : arr)
^^^