Solved

put in pointer address manually

Posted on 1998-09-08
7
178 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
How are result and Record declared?
0
 

Expert Comment

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

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now