?
Solved

Changing a integer to a char

Posted on 2004-09-24
9
Medium Priority
?
529 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
Industry Leaders: 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 380 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.
Suggested Courses

762 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