Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Changing a integer to a char

Posted on 2004-09-24
9
Medium Priority
?
531 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

604 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