Solved

Changing a integer to a char

Posted on 2004-09-24
9
526 Views
Last Modified: 2012-06-27
Ok im newer to C++. All I want to do is increment a pointer and display the address and corresponding value in the address. This will all occur in a loop so a range of values will be displayed. They will be displayed in a Listbox. I am having trouble with casting integers to chars etc. As well as passing the result to the listbox (an error closes the app because im sending the wrong type of string\int or watever) Here is my code: in the for loop is where the problems are. The main thing is getting temp to concatenate things right (casting integers and pointers to chars or pointers to chars or whatever).

void Scan(int Depth, int Number)
{
    SendMessage(hls_output,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"Beginning Scan...");
int i=0;
int *sptr=&i;
char temp;
for (i=0; i<Depth; i++)
{
    temp =  strcat(strcat((char)&sptr," : "),*(char)*sptr);
    *sptr++;
    SendMessage(hls_output,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)temp);
}  
   SendMessage(hls_output,LB_ADDSTRING,0,(LPARAM)(LPCTSTR)"Scan Complete");
}
0
Comment
Question by:Th3_GOD
[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
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12145016
It is very strange and confusing what you are trying to do. But as a general rule, to use strcat you need a buffer. I think you need sprintf instead:

void Scan(int Depth, int Number)
{
    SendMessage(hls_output,LB_ADDSTRING,0,(LPARAM)"Beginning Scan...");
    char temp[30];  // Put proper size here
    int i=0;
    int *sptr=&i;
    for (i=0; i<Depth; i++) {
        sprintf (temp, "%X : %i", sptr, *sptr);
        *sptr++;
        SendMessage(hls_output, LB_ADDSTRING, 0, (LPARAM)temp);
    }  
   SendMessage(hls_output,LB_ADDSTRING, 0, (LPARAM)"Scan Complete");
}
Hope this help,
Jaime.
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12145529
> int i=0;
> int *sptr=&i;
> char temp;
> for (i=0; i<Depth; i++)
> {
>    temp =  strcat(strcat((char)&sptr," : "),*(char)*sptr);
>    *sptr++;

I can't even make an intelligent guess as to what this code is trying to do.
If it compiles at all, it will produce numerous warnings.  If run, it will corrupt the
stack faster than you can say "What the ...?"

Please state in English words the problem you are trying to solve here, the inputs
and the desired output.

0
 
LVL 19

Expert Comment

by:drichards
ID: 12145718
What are you trying to do exactly?  sptr walks up, and potentially off, the stack.  If that's really what you're intending, then two small mods to jaime_olivares's code:

Use %p to format pointers and don't need * in *sptr++.  That just derefences the incremented pointer which is useless.  See comments in code.

void Scan(int Depth, int Number)
{
    SendMessage(hls_output,LB_ADDSTRING,0,(LPARAM)"Beginning Scan...");
    char temp[30];  // Put proper size here
    int i=0;
    int *sptr=&i;
    for (i=0; i<Depth; i++) {
        sprintf (temp, "%p : %i", sptr, *sptr);    // <-- Use %p to format pointers
        sptr++;                                               // Don't need *.  (*ptr)++ would increment i.
        SendMessage(hls_output, LB_ADDSTRING, 0, (LPARAM)temp);
    }  
   SendMessage(hls_output,LB_ADDSTRING, 0, (LPARAM)"Scan Complete");
}
0
Independent Software Vendors: 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 1

Author Comment

by:Th3_GOD
ID: 12156005
The basic function i want to do is create a memory scanner, ie a pointer in a loop that increments and displays the address and value in the address. Thats the basic function i want. I know this method will probably only scan values in the memory block of the application, and start only on the address of i, but thats ok. Depth is supposed to indicate how many times the pointer increments, , ie how many values to display. Number will be to show only addresses with values that are equal to the number, but i can do that. Im learning pointers and such and wanted to do a memory thing so this is what i came up with. I did get it to work in dos using the cout statement and ing the << for concatenation. Win32 is slightly different when dealing with numbers, and concatenation so thats where my problem lies tommorow i will test the codes you gave and see if they work.
   sptr++;                                               // Don't need *.  (*ptr)++ would increment i.

I dont want i incremented. I want the address of the pointer incremented.

I realize that a real memory scanner probably wouldnt work like this but thats ok.
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 95 total points
ID: 12156018
Based on your last comment, I think you need something like this:

void Scan(int Depth, int Number)
{
    SendMessage(hls_output,LB_ADDSTRING,0,(LPARAM)"Beginning Scan...");
    char temp[30];  // Put proper size here
    int i=0;
    int *sptr=&i;  
    for (i=0; i<Depth; i++) {
        if (*ptr==Number) {
            sprintf (temp, "%X : %i", sptr, *sptr);
            sptr++;
            SendMessage(hls_output, LB_ADDSTRING, 0, (LPARAM)temp);
        }
    }  
   SendMessage(hls_output,LB_ADDSTRING, 0, (LPARAM)"Scan Complete");
}
0
 
LVL 1

Author Comment

by:Th3_GOD
ID: 12161331
Looks perfect im working a bug out of this:

191 H:\C++ IDE\bin\Dev-Cpp\Proj\main.cpp implicit declaration of function `int sprintf(...)'

Im using the latest Dev C++ it does reconize the function but returns an error, the functions you made is right though and is what i need.
0
 
LVL 1

Author Comment

by:Th3_GOD
ID: 12161350
Nevermind had to include
#include "cstring"
#include "cstdio"

Now it works perfect Thanks.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12161367
try to #incude "stdio.h" and "string.h"
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12161378
Oh, sorry, too late for my comment
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

730 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