Solved

put in pointer address manually

Posted on 1998-09-08
7
188 Views
Last Modified: 2010-04-02
I have an unsigned short variable (result) which stores the address to a table, this variable holding the address is all I have to work with. All the fields are text data type. I want to take a char pointer and point it at the first field of the table but it squawks when I try to assign an unsigned short to a char *.
0
Comment
Question by:jtm082698
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 2

Accepted Solution

by:
milenvk earned 50 total points
ID: 1172289
That's because a pointer usually takes twice more memory than an unsigned short. Consider using with char* or void* or whatever pointer you'd like instead of using unsigned short. It really depends on the operating system that you are working with. Also you can work around the problem with the compiler warning or error if you are using explicit type cast, i.e. :

void fo(unsigned short table)
{
  char* pc = (char*) table;
  //....
}
0
 

Author Comment

by:jtm082698
ID: 1172290
Okay, but when I try to use it, i.e.:

ptrChar = (char*)result;
                              
for(i=0;i<sizeof(Record);i++)
{
Record[i] = *ptrChar;
ptrChar++;
}  

the compiler gives an unhandled access error.

0
 
LVL 2

Expert Comment

by:milenvk
ID: 1172291
If you are using C string you better loop up to the string length. The program crashes because you are trying to access a memory that you did not allocated. Use

for(i=0;i<sizeof(Record) && i < strlen(ptrChar)+1;i++)

for the loop.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 1172292
How are result and Record declared?
0
 

Expert Comment

by:dpms
ID: 1172293
result is an unsigned short variable and Record is a char [50]
0
 
LVL 84

Expert Comment

by:ozo
ID: 1172294
Is sizeof(unsigned short) <= sizeof(char *) on your machine?
Where did result get assigned with something you think would make (char*)result meaningful?
Could you be thinking of (char *)&result ?
But that would make ptrChar+=sizeof(Record) point beyond the end of result, so that doesn't make sense either.
Why isn't result declared as a char* ?
0
 
LVL 2

Expert Comment

by:milenvk
ID: 1172295
Even if unsigned short can handle a pointer, sizeof(Record) returns 50 and probably you are trying to access a memory that you did not allocated, i.e. if the result is a pointer to a memory of 30 allocated bytes, when i becomes 30 your program will crash with an access exception, like it does.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question