String Pointers

This is a discussion on String Pointers within the C Programming forums, part of the General Programming Boards category; I'm not quite sure how to explain this. I've got some string pointers which need to be filled with an ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    String Pointers

    I'm not quite sure how to explain this.

    I've got some string pointers which need to be filled with an unknown length string at run-time. What I'm having trouble with, is the best way to populate those strings.
    For example, I have a string pointer, which I'd like to populate thusly:

    sprintf(strptr2,"this string: %s is my %dth attempt at this", strptr1, int1);

    I know I need to Malloc or realloc to set the size of strptr2 before I sprintf to it. But how do I get the size?
    strlen ("this string: %s is my %dth attempt at this", strptr1, int1); doesn't work, though it's be nice if it did.
    I know I could make a fixed length string and sprintf to it, but in my application, the necessary length will be wildly variable.

    Is there an automatic way to make my own functions handle getting passed ("%d", int1) type of stuff?

    I know I'm missing something.

    Thanks.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    There are implementation-specific functions that can actually get the total size needed for that to work (I know Visual Studio has one), but otherwise it isn't possible without a manual method unfortunately.
    The best thing to do is to check the size of each strings, assume the maximum length for integers (like 20 chars or so) and then allocate enough space beforehand.
    You could, of course, also convert those numbers to strings and then check their length.
    Don't forget to add space for the whole formatting string you're passing, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course, it's not hard to produce a "close enough to work" estimate for this particular type of example: %d takes up a max of 12 characters, %s takes up as much as the string is long, and then the format string's length.

    You could write a parser that takes the arguments, like this:
    Code:
    int sprintfalloc(const char *fmt, ...)
    {
        va_list argp;
        const char *s = fmt;
        char *str;
        size_t size = 0; 
        va_start(argp, fmt);
        
        while(*s)
        {
            if (*s == '%')
            {
                 switch(s[1])
                 {
                     case 's': 
                        size += strlen((va_arg(argp, char *));
                        break;
                     case 'd':
                        size += 12;   // We could calculate the size of course. 
                        (void) va_arg(argp, int);
                        break;
                      case 'c':
                        size++;
                        break;
                      default:
                         // Do something here, perhaps?
                  }
            }
        }
        va_start(argp, fmt);
    
        str = malloc(size + 1);
        vsprintf(str, fmt, argp);
    
        va_end(argp);
    }
    Of course, it would get a whole lot more complicated if you needed to cope with more exotic variants of formatting, e.g "%10d" or "%10.19f", or worse yet "%*d". It can be dealt with, but at that point, it may make more sense to use a really large buffer, sprintf() to that buffer, and then copy to a smaller buffer if you need to keep the string around.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    If you have some kind of snprintf() function, you can get it to tell you how long the string would need to be.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. string pointers
    By David Korb in forum C++ Programming
    Replies: 3
    Last Post: 12-15-2001, 12:07 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21