Two different char constant arrays storing same data

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • plf
    New Member
    • Mar 2008
    • 2

    Two different char constant arrays storing same data

    Hi,

    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

    [CODE=cpp]
    #include<iostre am>
    #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(v oid *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","star 2","star3"};
    char const *value[]={"value1","val ue2","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);

    }

    [/CODE]

    File "myvar.h" contains following
    ---------------------------------------------
    [CODE=cpp]class myvar
    {

    public:
    int count;
    char const *a[];
    char const *b[];
    void getvar( char const *,char const *,int);
    };[/CODE]

    File "show.h" contains following
    ------------------------------------------------
    [code=cpp]class show
    {
    public:
    int x;
    void display(void *,int);
    };[/code]

    File "get.h" contains following
    ----------------------------------------------
    [CODE=cpp]class get
    {
    public:
    static void *myptr;
    get(void *objptr);
    get() ;
    void * returnptr();

    };

    void * get::myptr=NULL ;[/CODE]
    Last edited by Ganon11; Mar 21 '08, 03:47 PM. Reason: Fixing [CODE] tags.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are using C++.

    That means no casting.

    And you should read the article in the HowTos forum for C/C++ about the case against global variables.

    Post again with no casts and no global variable, and I 'll look at your code.

    Comment

    • plf
      New Member
      • Mar 2008
      • 2

      #3
      I would be very appreciative of you if you can help me out with the gobal variables and also typecasting inaddition to the character consant array problem.

      Thank you.

      Originally posted by weaknessforcats
      You are using C++.

      That means no casting.

      And you should read the article in the HowTos forum for C/C++ about the case against global variables.

      Post again with no casts and no global variable, and I 'll look at your code.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        First, you should read these articles:

        http://www.thescripts.com/forum/thread772412.html.

        http://www.thescripts.com/forum/thread737451.html.

        Comment

        Working...