Results 1 to 6 of 6

Thread: adding stuff to the beginning of arrays?

  1. #1

    Thread Starter
    Lively Member dubae524's Avatar
    Join Date
    Jun 2001
    Location
    Currently on this Super Star Destroyer being telekinetically strangled to death by Darth Vader
    Posts
    78

    adding stuff to the beginning of arrays?

    Let's say I have a char array, okay?

    I initialize it:

    char gerbil[255] = "mike tyson";

    But let's say that, later in the program, I want to add the following string to the beginning of it:

    "that stupid ear-biting "

    so this would be true:

    gerbil == "that stupid ear-biting mike tyson"

    any answers?
    - Justin Patrick Butler

    Comme je trouve. "As I find."
    - Butler family quote

    Beneficia sumptos procul superant. "The benefits far exceed the costs."
    - Myself

  2. #2
    Lively Member
    Join Date
    May 2000
    Posts
    123
    create a new string:

    char hamster[] = "that stupid ear-biting " ;

    then concatanate the two:

    char hamger[100];

    hamger = strcat(hamster, gerbil);

    ps - you gotta use <string.h> for this

  3. #3

    Thread Starter
    Lively Member dubae524's Avatar
    Join Date
    Jun 2001
    Location
    Currently on this Super Star Destroyer being telekinetically strangled to death by Darth Vader
    Posts
    78
    Oh, thanks!
    - Justin Patrick Butler

    Comme je trouve. "As I find."
    - Butler family quote

    Beneficia sumptos procul superant. "The benefits far exceed the costs."
    - Myself

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    that won't work.
    it will append the content of gerbil to the buffer of hamster (which is too small) and then assign the memory address of hamster to hamger (probably a syntax error). The correct way would be:
    Code:
    // create a new string: 
    char hamster[100] = "that stupid ear-biting " ; 
    // then concatanate the two: 
    strcat(hamster, gerbil);
    Now hamster contains the complete string. You must make sure that hamster is large enough to hold both.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Lively Member
    Join Date
    May 2000
    Posts
    123
    yep i was wrong about the assignment but you can definately declare an array dynamically and then still strcat onto it.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    yeah, but your code didn't allocate an array dynamically...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width