delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/03/19/20:01:46

Message-ID: <3AB6AA85.A20AF326@earthlink.net>
From: Martin Ambuhl <mambuhl AT earthlink DOT net>
X-Mailer: Mozilla 4.76 [en] (Win95; U)
X-Accept-Language: en,zh-CN,fr,de-CH,ru
MIME-Version: 1.0
Newsgroups: comp.os.msdos.djgpp
Subject: Re: ASCII codes
References: <EZwt6.157$k DOT 40949 AT sapphire DOT mtt DOT net>
Lines: 54
Date: Tue, 20 Mar 2001 00:52:38 GMT
NNTP-Posting-Host: 63.210.220.151
X-Complaints-To: abuse AT earthlink DOT net
X-Trace: newsread1.prod.itd.earthlink.net 985049558 63.210.220.151 (Mon, 19 Mar 2001 16:52:38 PST)
NNTP-Posting-Date: Mon, 19 Mar 2001 16:52:38 PST
Organization: EarthLink Inc. -- http://www.EarthLink.net
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

jaystewart1 wrote:
> 
> Can you guys help me write a program that outputs two strings and their
> length, then outputs the two strings in alphabetical order?

Show us (better: show comp.lang.c or alt.comp.lang.learn.c-c++) your
attempt and we will try to help fix your code.

> 
> The array below represents the ASCII codes for a string. Can anyone help me
> write a program that outputs the corresponding string?
> 
> int x[] ={65,112,114,105,108,32,115,104,111,119,
>               101,114,115,32,98,114,105,110,103,32,77,97,
>               121,32,102,108,111,119,101,114,115,32,33,0};

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int x[] =
        { 65, 112, 114, 105, 108, 32, 115, 104, 111, 119, 101, 114, 115,
        32, 98, 114, 105, 110, 103, 32, 77, 97, 121, 32, 102, 108,
        111, 119, 101, 114, 115, 32, 33, 0
    };
    char *y;
    size_t i;
    if (!(y = malloc(sizeof x / sizeof *x))) {
        printf("Damn!\n");
        exit(EXIT_FAILURE);
    }
    for (i = 0; i < sizeof x / sizeof *x; i++)
        y[i] = x[i];
    printf
        ("The 'correctness' of the following output depends\n"
         "on the implementation using ASCII, by no means\n"
         "guaranteed by the standard.  Here we go:\n %s\n\n", y);
    printf
        ("Note that 'y' in the above statement could not be replaced by\n"
         "(char *)x because x[0] interpreted as chars is \\%03o \\%03o\n",
         (char)*(char *)&x, (char)(*(1 + (char *)&x)));
    free(y);
    return 0;
}


The 'correctness' of the following output depends
on the implementation using ASCII, by no means
guaranteed by the standard.  Here we go:
 April showers bring May flowers !

Note that 'y' in the above statement could not be replaced by
(char *)x because x[0] interpreted as chars is \101 \000

- Raw text -


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