473,396 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Two different char constant arrays storing same data

2
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

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include "myvar.h"
  3. #include "show.h"
  4. #include "get.h"
  5.  
  6. using namespace std;
  7.  
  8.  
  9. void myvar::getvar( char const *x, char const *y,int h)
  10. {
  11.  
  12. b[h]=y;
  13. a[h]=x;
  14.  
  15. cout << "Address of a= " << &a[h] << endl;
  16. cout << "Address of b= " << &b[h] << endl;
  17.  
  18. }
  19.  
  20. void show::display(void *p,int f)
  21. {
  22.  
  23. myvar *n = (myvar*)p;
  24. for(int i=f;i>-1;i--)
  25. {
  26. cout << "n->a= " << n->a[i] << endl <<endl;
  27. cout << "n->b= " << n->b[i] << endl <<endl;
  28. }
  29. }
  30.  
  31. get::get(void *objptr)
  32. {
  33.  
  34. myptr = (myvar*)objptr;
  35.  
  36. }
  37.  
  38. get::get()
  39. {
  40. }
  41.  
  42. void * get::returnptr()
  43. {
  44. return myptr;
  45. }
  46.  
  47.  
  48. main(void)
  49. {
  50.   myvar v;
  51.   void *ptr=&v;
  52.   int i=0;
  53.  
  54.      char const *stars[]={"star1","star2","star3"};
  55.      char const *value[]={"value1","value2","value3"};
  56. for(i=0;i<3;i++)
  57.  {
  58.  
  59.   v.getvar(stars[i],value[i],i);
  60.  }
  61.   get c(ptr);
  62.   get d;
  63.   void *s;
  64.   s=d.returnptr();
  65.   show a;
  66.   a.display(s,i-1);
  67.  
  68. }
  69.  
  70.  
File "myvar.h" contains following
---------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. class myvar
  2. {
  3.  
  4.  public:
  5.          int count;
  6.          char const *a[];
  7.          char const *b[];
  8.          void getvar( char const *,char const *,int);
  9. };
File "show.h" contains following
------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. class show
  2. {
  3.  public:
  4.         int x;
  5.         void display(void *,int);
  6. };
File "get.h" contains following
----------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. class get
  2. {
  3.  public:
  4.        static void *myptr;
  5.        get(void *objptr);
  6.        get() ;
  7.        void * returnptr();
  8.  
  9. };
  10.  
  11. void * get::myptr=NULL;
Mar 21 '08 #1
3 1557
weaknessforcats
9,208 Expert Mod 8TB
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.
Mar 21 '08 #2
plf
2
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.

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.
Mar 21 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For...
30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
3
by: Ian Arnold | last post by:
Alright, so I know that you can use char pointers to store strings, and I'm trying to make a very simple program to see how it all works that will have the user enter 5 characters, then the...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
28
by: Merrill & Michele | last post by:
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void){ char *p; p=malloc(4); strcpy(p, "tja"); printf("%s\n", p); free(p); return 0;
9
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers,...
8
by: machikelxol | last post by:
I'm having a strange error when I try reading from a file. here is the code: buffstring values = 1, 2, 3, 4 and then it crashes afterwards on the 4th iteration. This works fine until the fourth...
4
by: nass | last post by:
hello everyone, i have a bit of problem reading char * strings from a buffer (a shared memory, pointed to by 'file_memory'). basically i have a structure in memory 'ShMem' that can be accessed by...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.