Solved

put in pointer address manually

Posted on 1998-09-08
7
191 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
Technology Partners: 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

Technology Partners: 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!

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

691 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