Thread: Creating a long, specific, c string

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Just a tiny correction in John's example (which isn't wrong! just redundant)... This:
    Code:
    char s[SIZE + 1] = { 0 };
    Will fill the entire array with zeros, but this:
    Code:
    memset( s, ' ', SIZE );
    Will fill the array again, this time, with spaces.

    The compiler will not "optimize" this. The faster code should be:
    Code:
    char s[SIZE + 1];    // no initializer
    memset( s, ' ', SIZE );
    s[SIZE] = '\0';
    
    s[0] = 'x';
    s[SIZE - 1] = 'y';
    PS: And his solution using macros is very nice!

    []s
    Fred
    Last edited by flp1969; 08-31-2022 at 05:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating files in a specific directory
    By hinesro in forum C Programming
    Replies: 5
    Last Post: 02-01-2015, 01:36 AM
  2. Replies: 16
    Last Post: 08-01-2012, 12:14 AM
  3. problem creating program to the specific specification
    By rushhour in forum C++ Programming
    Replies: 22
    Last Post: 11-28-2008, 12:15 AM
  4. unsigned long long to string conversion
    By Wiretron in forum C++ Programming
    Replies: 6
    Last Post: 12-21-2007, 04:02 AM
  5. Creating files in specific directories
    By Kyoto Oshiro in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 08:50 PM

Tags for this Thread