Solved

put in pointer address manually

Posted on 1998-09-08
7
183 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 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