Solved

Changing a integer to a char

Posted on 2004-09-24
9
522 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
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
 
LVL 1

Author Comment

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

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 95 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Nevermind had to include
#include "cstring"
#include "cstdio"

Now it works perfect Thanks.
0
 
LVL 55

Expert Comment

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

Expert Comment

by:Jaime Olivares
Comment Utility
Oh, sorry, too late for my comment
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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

13 Experts available now in Live!

Get 1:1 Help Now