www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/26/16:00:28

From: nil AT hem1 DOT passagen DOT se (Nicklas Lindgren)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointing into a string
Date: Sun, 26 Jul 1998 19:37:31 GMT
Organization: Algonet/Tninet
Lines: 47
Message-ID: <35bb828f.9376142@news.algonet.se>
References: <35b91d1a DOT 2313606 AT news DOT algonet DOT se> <35B92DF2 DOT 2704E553 AT alcyone DOT com>
NNTP-Posting-Host: du242-7.ppp.algonet.se
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Fri, 24 Jul 1998 17:59:30 -0700, Erik Max Francis <max AT alcyone DOT com>
wrote:

>Nicklas Lindgren wrote:
>
>> I want to store an int inside a string. (It must be a string, not a
>> struct or anything else)
>> 
>> I've tried to make a pointer of int type into the string but i can't
>> figure out how to make it work.
>> 
>> If anyone can tell me how to, or suggest something better, i'd
>> appreciate it.
>
>What do you mean, "store an int inside a string"?  Do you mean have a
>strange which contains the ASCII equivalent of an int?  In that case,
>you should sprintf:
>
>    int i; /* the int you want "converted" */
>    char buffer[32]; /* more than you need */
>
>    sprintf(buffer, "%d", i);
>


I'll try that if i can't find a way to make this work:

What i was looking for was a way to store the actual memory
representation of the int in the same memory space as the string. (The
string doesn't have to be printable)

I've tried making a pointer to a char in the string, and then
implicitly converting it to an int. I used something like:

char string[255];
int x = 5;
*(int *)&string[0] = x;

// And for testing that it works:
x = *(int *)&string[0];
cout << x;

The same number comes out in the other end, but it doesn't seem to end
up in the string.

	Nicklas Lindgren

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019