www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/27/07:30:23

From: rjvdboon AT cs DOT vu DOT nl (Boon van der RJ)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Pointing into a string
Date: 27 Jul 1998 11:10:43 GMT
Organization: Fac. Wiskunde & Informatica, VU, Amsterdam
Lines: 48
Message-ID: <6phn7j$b2h$1@star.cs.vu.nl>
References: <35b91d1a DOT 2313606 AT news DOT algonet DOT se> <35B92DF2 DOT 2704E553 AT alcyone DOT com> <35bb828f DOT 9376142 AT news DOT algonet DOT se> <35BC12C3 DOT 56CDD310 AT alcyone DOT com>
NNTP-Posting-Host: sloep11.cs.vu.nl
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Erik Max Francis (max AT alcyone DOT com) wrote:
> Nicklas Lindgren wrote:
> > I've tried making a pointer to a char in the string, and then
> > implicitly converting it to an int. I used something like:
> > 
[Nicklas' example proggie deleted]
> > 
> > The same number comes out in the other end, but it doesn't seem to end
> > up in the string.
> 
> Of course it doesn't.  ints and NUL-terminated character strings are not
> the same thing.

That is very true. If you _really_ want to do it, try a union of an
int and a string. Take a basic_learn_how_to_C-book and read the union
section.
The example program at the end, compiled and run on SUN unix
machines the program at the end gives as output:
0x12 0x34 0x56 0x78 0x0 
0x0 0x34 0x56 0x78 0x0 
It will probably be reversed on a PC.

hth,
 Robert
====== start example prog ======
#include <stdio.h>
#define IS sizeof(int)
union xx {
  int jan;
  char nix[IS+1];
};
union xx xxx;

int main() {
  unsigned i;
  xxx.nix[IS]='\0';
  xxx.jan = 0x12345678;
  for (i=0; i < IS+1; i++)   printf("0x%x ", xxx.nix[i]);
  printf("\n");
  xxx.jan = 0x345678;
  for (i=0; i < IS+1; i++)   printf("0x%x ", xxx.nix[i]);
  printf("\n");
  return 0;
}
==== end example prog ====
-- 
rjvdboon AT cs DOT vu DOT nl        | "En dat is niet waar!" sprak (ex?) Staatsecre-
www.cs.vu.nl/~rjvdboon   |    taris Netelenbos (onderwijs) fel.

- Raw text -


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