The problem that I am faced with in the following code is that the two character constant arrays "a" and "b" end up containing the same data. For example, in
"void myvar::getvar( char const *x, char const *y,int h)" function the storage order is, "b" is first and "a" is second , hence "b" also ends up containing same data as "a". But if the order is changed .i.e "a" is first and "b" is second then "a" ends up containing same data as "b". What could be wrong? Thanks.
Following is the code
Expand|Select|Wrap|Line Numbers
- #include<iostream>
- #include "myvar.h"
- #include "show.h"
- #include "get.h"
- using namespace std;
- void myvar::getvar( char const *x, char const *y,int h)
- {
- b[h]=y;
- a[h]=x;
- cout << "Address of a= " << &a[h] << endl;
- cout << "Address of b= " << &b[h] << endl;
- }
- void show::display(void *p,int f)
- {
- myvar *n = (myvar*)p;
- for(int i=f;i>-1;i--)
- {
- cout << "n->a= " << n->a[i] << endl <<endl;
- cout << "n->b= " << n->b[i] << endl <<endl;
- }
- }
- get::get(void *objptr)
- {
- myptr = (myvar*)objptr;
- }
- get::get()
- {
- }
- void * get::returnptr()
- {
- return myptr;
- }
- main(void)
- {
- myvar v;
- void *ptr=&v;
- int i=0;
- char const *stars[]={"star1","star2","star3"};
- char const *value[]={"value1","value2","value3"};
- for(i=0;i<3;i++)
- {
- v.getvar(stars[i],value[i],i);
- }
- get c(ptr);
- get d;
- void *s;
- s=d.returnptr();
- show a;
- a.display(s,i-1);
- }
---------------------------------------------
Expand|Select|Wrap|Line Numbers
- class myvar
- {
- public:
- int count;
- char const *a[];
- char const *b[];
- void getvar( char const *,char const *,int);
- };
------------------------------------------------
Expand|Select|Wrap|Line Numbers
- class show
- {
- public:
- int x;
- void display(void *,int);
- };
----------------------------------------------
Expand|Select|Wrap|Line Numbers
- class get
- {
- public:
- static void *myptr;
- get(void *objptr);
- get() ;
- void * returnptr();
- };
- void * get::myptr=NULL;