Thread: strings in C

  1. #1
    tsorial
    Guest

    strings in C

    Given a string char *s:

    How do you print it the string to the terminal one character at a time.

    Your help is greatly appreciated.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    well, I don't do much single char putting but you could do:
    for(i = 0; i < strlen(string); i++)
    printf("%c", string[i]);
    //or...
    putchar(string[i]);

    ..or something to that effect.

    or,

    while(*string)
    putchar(*string++);

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, just increment the pointer to point to the next char and output that char. Understand? Need code?
    1978 Silver Anniversary Corvette

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, you must point to something!
    1978 Silver Anniversary Corvette

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Originally posted by vVv
    Would crash otherwise.
    Not necessarily. It only might crash if you try to alter the contents of a literal, which is not happening here. I wouldn't bother allocating space for a string on the heap before doing anything with it, unless it's length (or existence) isn't known until runtime. Either use the stack or a literal.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by Sorensen


    Not necessarily. It only might crash if you try to alter the contents of a literal, which is not happening here. I wouldn't bother allocating space for a string on the heap before doing anything with it, unless it's length (or existence) isn't known until runtime. Either use the stack or a literal.
    Sorensen, I beg to differ. I always thought that if you try to view/change unallocated memory (unnamed memory) that it is illegal at that point...You can go no further...

    Garfield
    1978 Silver Anniversary Corvette

  7. #7
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What unallocated memory? If it's a dangling pointer then this is true, but something like Sebastiani's code would work for -

    char * x = "literal string";
    char y[] = "stack string";

    So there is no need to manually allocate memory, other than for the reasons I stated originally.

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    >> char * x = "literal string"; <<

    This would be allocating the memory at declaration time...
    1978 Silver Anniversary Corvette

  9. #9
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >This would be allocating the memory at declaration time...

    Erm.. no it wouldn't. It's taking a pointer and initialising it with the address of a string literal, which already resides in memory. Check the address of the string, it won't be on the stack or the heap, but in one of the data segments.

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by Sorensen
    >This would be allocating the memory at declaration time...

    Erm.. no it wouldn't. It's taking a pointer and initialising it with the address of a string literal, which already resides in memory. Check the address of the string, it won't be on the stack or the heap, but in one of the data segments.
    Okay, I see...

    Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM