Solved

put in pointer address manually

Posted on 1998-09-08
7
184 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
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

829 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