🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.lang.c++
5 messages
5 total messages Started by bastards Tue, 10 Sep 1996 00:00
Question for C Programmers...
#5
Author: bastards
Date: Tue, 10 Sep 1996 00:00
15 lines
1311 bytes
Hi!  First : I'm new to C language.  :)

Second:  I would like to know if somebody could tell me how I could add a
Caractere "0" after a string... something like:

My_String = "A_TEST" + CHR(10);

But, unfortunatly the "CHAR" function dosn't exist...  :(   so what should
I do?

THanks a lot folks! 		


		Ed.
Re: Question for C Programmers...
#6
Author: Karsten Roemke Zi.209 PIB
Date: Wed, 11 Sep 1996 00:00
45 lines
2546 bytes
bastards (bastards@ntic.qc.ca) wrote:
: Hi!  First : I'm new to C language.  :)

: Second:  I would like to know if somebody could tell me how I could add a
: Caractere "0" after a string... something like:

: My_String = "A_TEST" + CHR(10);

: But, unfortunatly the "CHAR" function dosn't exist...  :(   so what should
: I do?
Maybe not the best solution, but one which works:
#include <stdio.h> /* input output*/
#include <stdlib.h> /* malloc  (and more) */
#include <string.h> /* strlen (and more)*/
    main()
    {
      char *test;
      int i;

      test = (char *) malloc(strlen("test")+2); /* +2 for the \0 and the 
                            extra character you want to use */
      sprintf(test, "%s%c\0","test",10);
      for(i=0;i<strlen(test)+1;i++)
      {
         printf("%c %d\n",test[i],test[i]));
      }
    }
output:
t 116
e 101
s 115
t 116
 
 10
 0
10 written as character is linefeed.

Maybe you should read a book about C. 
(kernighan and ritchie is nice)
                                                  -----------------------------  
                                    Karsten Roemke
                                    email : roemke@space9.physik.uni-bonn.de
                                    phone : 0228 212395
                                 -----------------------------------------------
Re: Question for C Programmers...
#7
Author: Kurt Watzka
Date: Wed, 11 Sep 1996 00:00
44 lines
2449 bytes
"bastards" <bastards@ntic.qc.ca> writes:

>Hi!  First : I'm new to C language.  :)

>Second:  I would like to know if somebody could tell me how I could add a
>Caractere "0" after a string... something like:

>My_String = "A_TEST" + CHR(10);

The problem is not how to get a character from the numeric value 10.
'\xA' is an integer constant that is what you are looking for. 
When adding a '0' after a string, the problem is not how to represent
the character '0'. 

Your _real_ problem is that
 
 char * My_string = "This is a string" + '\xA';

is a strange way to get a pointer to the string "string" in C. Adding
something to a string literal does not do what you expect it to do.

If the buffer that holds the string is big enough, the following
piece of code should do what you want:

   char My_string[BIG_ENOUGH] = "This is a string";

   char *p = strchr(My_string, 0);
   *p++ = 10;
   *p   = 0;
 
>But, unfortunatly the "CHAR" function dosn't exist...  :(   so what should
>I do?

Regarding the conversion of integers to characters, the conversion
is done automatically, and the "only" caveat is that the integer
value must not exceed the domain (range of representable values) 
of char.

Kurt

-- 
| Kurt Watzka                             Phone : +49-89-2180-6254
| watzka@stat.uni-muenchen.de
Re: Question for C Programmers...
#8
Author: Marc Boily
Date: Wed, 11 Sep 1996 00:00
42 lines
1819 bytes
it's simple !!!

char *example;

main()
{
	example = (char *)malloc( 10 + 1 );   /* +1 for the NULL */
	
	strcpy( example, "abcdefghij" );
	example[10] = 0x00;                       /* 10 because the first is 0 */
                                                                /* 0x00 =
\0 */
}

-- 
Marc Boily
Marc.Boily@CGI.CA
:-)

bastards <bastards@ntic.qc.ca> wrote in article
<01bb9f5e$eb2c1ea0$5caceccd@bastards.ntic.qc.ca>...
> Hi!  First : I'm new to C language.  :)
> 
> Second:  I would like to know if somebody could tell me how I could add a
> Caractere "0" after a string... something like:
> 
> My_String = "A_TEST" + CHR(10);
> 
> But, unfortunatly the "CHAR" function dosn't exist...  :(   so what
should
> I do?
> 
> THanks a lot folks! 		
> 
> 
> 		Ed.
> 
> 
> 
> 
> 
Re: Question for C Programmers...
#9
Author: Andreas Stolle
Date: Sat, 14 Sep 1996 00:00
50 lines
2146 bytes
Marc Boily wrote:
> 
> it's simple !!!
> 
> char *example;
> 
> main()
> {
>         example = (char *)malloc( 10 + 1 );   /* +1 for the NULL */
> 
>         strcpy( example, "abcdefghij" );
>         example[10] = 0x00;                       /* 10 because the first is 0 */
>                                                                 /* 0x00 =
> \0 */
> }
> 
> --
> Marc Boily
> Marc.Boily@CGI.CA
> :-)
> 
> bastards <bastards@ntic.qc.ca> wrote in article
> <01bb9f5e$eb2c1ea0$5caceccd@bastards.ntic.qc.ca>...
> > Hi!  First : I'm new to C language.  :)
> >
> > Second:  I would like to know if somebody could tell me how I could add a
> > Caractere "0" after a string... something like:
> >
> > My_String = "A_TEST" + CHR(10);
> >
> > But, unfortunatly the "CHAR" function dosn't exist...  :(   so what
> should
> > I do?
> >
> > THanks a lot folks!
> >
> >
> >               Ed.
> >
> >
> >
> >
> >

Even simplier:

char *test="abc\012";

MFG Andreas
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads