Solved

Changing a integer to a char

Posted on 2004-09-24
9
528 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 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

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!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

695 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