Re: What's wrong with the string?

From: Freddy (blahhblaah_at_hotmail.com)
Date: 06/20/04


Date: Sun, 20 Jun 2004 20:45:27 +0200

Hi Frank,

> void example() {
> char *str = "this is a test";

Not an answer to your question, but I would suggest you change the
latter line to:
    char str[] = "this is a test";

The difference is that this option actually claims 15 bytes of memory
for you.
Your option just claims memory for a pointer, and the location it points
to (probably NULL) will be filled with 'this is a test'.

> while (*str != 0) {
> putc(*str++);

putc is used to put a character from a stream to the screen (see man
putc). You might want to use:
     putchar(*str++);

> }
> return;
> }
>
> putc() is a function to put a char on the serial console.
>
> This function ends up with a strange string on the serial console
> "><...HH..>>..."
>
> But if I modify the function to the following one:
>
> void example_modified() {
> char str[10] = \
> {'t','h','i','s',' ','i','s',' ','a',' ','t','e','s','t','\0'};

I would recoomend to leave the 10 out of the declaration. This will be
filled in correctly by your compiler if you leave it out. In your case
you fill it with 15 characters, which means you are writing memory that
isn't yours!
By the way, doing:
     char str[]="this is a test";
works as well as:
     char str[]={'t','h','i','s',' ','i','s',' ','a',
                 ' ','t','e','s','t','\0'};

> int i = 0;
> for (i = 0; str[i] != 0; i++) {
> putc(str[i]);
> }
> return;
> }
>
> Then it works. So I am wondering if there is something wrong with the
> stack setting. Can anybody tell me what makes the above two functions
> different? Why using char array str[10] works while using pointer str*
> doesn't work?

See above.

Good luck!
Freddy



Relevant Pages

  • Re: both specifying now, Khalid and Hamza pined the recent tents in search of current notion
    ... equally squeeze Hakeem and Annie's protestant draper. ... rain insights. ... Gawd, I'll project the character. ... it will apparently guide the memory. ...
    (sci.crypt)
  • Re: Cohens paper on byte order
    ... memory space representing that variable. ... >> I meant by character by character.) ... > It is used for practical tasks by a lot of engineers working with ...
    (sci.crypt)
  • Re: Dynamic Disassembler (determine main() location at runtime)
    ... The current dead space seems to be 0x00 and 0xCC on ... Windows 2000 allocates Virtual Memory on 64KB boundaries for the ... may see more dead space with a new BYTE fill character. ... I don't see why you would care if there is a jump stub for main or not; ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Read from a file
    ... > I having a problem reading all characters from a file. ... the memory buffer that you want to contain the data read from the file. ... Then, when you want to print that value, it's a single character you ... want to print instead of a string. ...
    (comp.lang.c)
  • Re: Decoding strategy
    ... Does File Stream caches contents of file in memory? ... FileStream does buffer, which is in a sense a kind of caching. ... beginning of character, or maybe end of "previous" character? ...
    (microsoft.public.dotnet.languages.csharp)