Thread: How do I add to a pointer string?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    58

    How do I add to a pointer string?

    Say this is my input..

    4 WORD word word word
    2 word word

    the number is how many words are in the line. I need to know how to add the words to a pointer string.
    Code:
    char *string;
    
    for(i=1; i<x; i++){
    strcat(string, array[i])
    }
    this gives me a seg fault.... obviously because I have no memory available bc its just a pointer..
    but I dont know how to write it.. please help

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, you could give string some space.
    Code:
    char string[100];  /* declare string to be an array of 100 characters */
    BTW, you should either use strcpy() the first time, instead of strcat, or initialize the string to be empty. For example:
    Code:
    char string[100] = "";
    
    /* ... */
    Also: for loops generally start counting at 0, not 1. But since you're missing a semicolon, I'm guessing you typed it out by hand, so maybe your original version had 0 in it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by dwks View Post
    Well, you could give string some space.
    Code:
    char string[100];  /* declare string to be an array of 100 characters */
    BTW, you should either use strcpy() the first time, instead of strcat, or initialize the string to be empty. For example:
    Code:
    char string[100] = "";
    
    /* ... */
    Also: for loops generally start counting at 0, not 1. But since you're missing a semicolon, I'm guessing you typed it out by hand, so maybe your original version had 0 in it.

    I cant initialize the string like that because it wastes memory. Also my loop has to start at 1 because if array[0] is 4 in the first line.. and its 2 in the second line. Those are the numbers that I skipped over bc I dont want to add those to my string. How do I do it using the pointer string?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by scarlet00014 View Post
    I cant initialize the string like that because it wastes memory. Also my loop has to start at 1 because if array[0] is 4 in the first line.. and its 2 in the second line. Those are the numbers that I skipped over bc I dont want to add those to my string. How do I do it using the pointer string?
    Are you working on a watch? Or something else that only has a few hundred bytes of memory?

    Modern PC's even Mobile phones and other mid-size embedded devices, it will probably waste MORE memory to allocate dynamically and track the allocation, than it does to have a static size - unless of course you have MANY strings, and the strings vary dramatically from a few characters all the way to hundreds or thousands.

    I know that everyone should be careful with memory, but there is actually good reason to NOT use pointers and dynamic memory allocation in every situation where the size of the data is unknown.

    Typically, a call to malloc takes several hundred if not thousand clock-cycles (and in worst case it's MUCH more than that, but fortunately, that is rare) - allocating 100 bytes on the stack is one or two instructions - filling it with zero is of course a bit more, but allocating the space via malloc doesn't fill it with zero either, so you neither gain nor loose there. The space used internally by malloc is often 32 or 64 bytes, so a small allocation will still take up quite significant amounts of space (the embedded OS I work with uses 12 bytes, and that is very small compared to Linux or Windows - and for a x86 processor to be efficient, the allocation MUST be at least 8 byte aligned to work with double, and 16-byte aligned to allow SSE instructions to flow smoothly). So allocating memory for very small strings is really "penny-wise, pound foolish" to use an English expression - you are saving on the small bits, but loosing out in the overall scheme of things.

    --
    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.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by matsp View Post
    Are you working on a watch? Or something else that only has a few hundred bytes of memory?

    Modern PC's even Mobile phones and other mid-size embedded devices, it will probably waste MORE memory to allocate dynamically and track the allocation, than it does to have a static size - unless of course you have MANY strings, and the strings vary dramatically from a few characters all the way to hundreds or thousands.

    I know that everyone should be careful with memory, but there is actually good reason to NOT use pointers and dynamic memory allocation in every situation where the size of the data is unknown.

    Typically, a call to malloc takes several hundred if not thousand clock-cycles (and in worst case it's MUCH more than that, but fortunately, that is rare) - allocating 100 bytes on the stack is one or two instructions - filling it with zero is of course a bit more, but allocating the space via malloc doesn't fill it with zero either, so you neither gain nor loose there. The space used internally by malloc is often 32 or 64 bytes, so a small allocation will still take up quite significant amounts of space (the embedded OS I work with uses 12 bytes, and that is very small compared to Linux or Windows - and for a x86 processor to be efficient, the allocation MUST be at least 8 byte aligned to work with double, and 16-byte aligned to allow SSE instructions to flow smoothly). So allocating memory for very small strings is really "penny-wise, pound foolish" to use an English expression - you are saving on the small bits, but loosing out in the overall scheme of things.

    --
    Mats
    Thanks for all the info.. but it doesnt really help with my question.. Im taking a class and they specificaly said we werent allowed to initialize it.. lol.. and ya my test file is longer than 2 lines..

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    HuH?
    You can't initialize arrays (you're not allowed to) or you can't waste memory (you aren't allowed to and/or do not want to)?
    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.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So
    char string[100];
    string[0] = 0;

    Then you can strcat up to 99 chars to it.

    You'd have to do as much with malloc anyway.

    OTOH, if you're reading a file, perhaps malloc is better.
    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.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    How do you suppose memory is acquired? You could do something like this:

    Example:
    Code:
    #include <stdarg.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *multistrcat(const char *a, ...)
    {
      va_list list;
      char *r;
      const char *next;
      size_t length = 1 + strlen(a);
    
      va_start(list, a);
      for(next = (const char *)va_arg(list);next;next = (const char *)va_arg(list))
        length += strlen(next);
      va_end(list);
    
      r = malloc(length);
      if(!r)
        return 0;
    
      strcpy(r, a);
    
      va_start(list, a);
      for(next = (const char *)va_arg(list);next;next = (const char *)va_arg(list))
        strcat(r, next);
      va_list(end);
    
      return r;
    }
    No "wasted" memory.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Quote Originally Posted by Elysia View Post
    HuH?
    You can't initialize arrays (you're not allowed to) or you can't waste memory (you aren't allowed to and/or do not want to)?
    this particular array. no they want us to do it another way. I figured it out.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by scarlet00014 View Post
    this particular array. no they want us to do it another way. I figured it out.
    Care to explain how you did it since methinks that the final program looks similar to the one master5001 has posted. Just curious btw.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Replies: 10
    Last Post: 11-06-2005, 09:29 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM