Confusion with structure variable in C++ [closed] - c++

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.

Related

same address for different variables of different functions in c [closed]

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.

Pointer to function with defined type [closed]

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 9 years ago.
Improve this question
I have function declaration as int StartSale(CTXNSession & txnsession) where CTXNSession is a class.
I need to create pointer to this function.
I tried to achieve it like this:
int (*pFct)(CTXNSession&);
But I got compilation error.
But if the argument was of a predefined type , there was no error.
Please assist
Your declaration of function pointer is correct. Perhaps you forgot to declare the class? Or maybe some other error.
This code compiles without errors:
class CTXNSession; // your class
int StartSale(CTXNSession & txnsession) {} // your function
int main() {
int (*pFct)(CTXNSession&); // function pointer declaration
pFct = StartSale; // assignment for a bonus
return 0;
}

Why won't my code compile? [closed]

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
This is a code that takes a series of 3 numbers in a number pattern and figures out the difference between them. everything seems to be right but my compiler keeps telling me I need an initializer before int i? sorry, I'm new to C++ so I'm sure my code is horrible.
using namespace std;
void add(int a, int b, int c)
int i;
for (a+i!=b;b+i!=c)
{i=0; i<100; i++;}
else {cout i;}
};
int main()
{
int x, y, z;
cin>>x;
cin>>y;
cin>>z;
add(x, y, z);
}
Many things, first you're missing a curly braze after your add function.
Also, you have one extra ; in your for declaration.
Also, after your function add there shouldn't be a ;

c++ vector of struct, why can't i use vector.at(i) or vector[i] for struct? [closed]

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 8 years ago.
Improve this question
struct info{
int num;
int weight;
};
vector<info> nbr;
the above declarations are global, and I have a loop in my function that looked like this
for(int i=0;i<nbr.size();i++){
info i = nbr.at(i);
vector<int> v;
v.push_back(i.weight);
tb[i.num] = v;
}
info i = nbr.at(i) gives me error: no matching function for call to 'std::vector<info, std::allocator<info> >::at(info&)'
why? what did I do wrong?
for(int i=0;i<nbr.size();i++){
info i = nbr.at(i);
This is why.
You're redefining what i refers to.
It's both your loop index int i and info i, and your compiler is wondering why you're passing an info& to vector::at.
Change the name and you should be good to go.

std::map operation by value or pointer? [closed]

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
I has a class A:
struct A
{
std::map<int,int> aa;
}
and a class B:
struct B
{
std::map<int,B> bb;
}
If those code in a infinite loop:
B b;
int inx=0;
while(true)
{
A a;
a.aa[0] = 0;
b.bb[inx] = a;
++inx;
}
Will that cause memory leak ?
I want all values of a copy to b[inx]. I just want to know that is the implementaion of std::map pointer ?
Will that cause memory leak ?
There are no memory leak in your program, but a compilation error, since there are no operator[] defined for struct B.
Assuming you add to map here:
while(true)
{
A a;
a[0] = 0;
b[inx] = a;
++inx;
}
there are no memory leaks. The memory will increase until you use all int numbers, but that's normal, since they have to be stored somewhere in memory.