a little help with array to string

This is a discussion on a little help with array to string within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to make a function that will extract a range of elements from an array and make a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    a little help with array to string

    Hi, I'm trying to make a function that will extract a range of elements from an array and make a string out of them...program keeps crashing after exacution.... any suggestions?

    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    
    char * gav (char * arg[], int start, int end) {
         char * z;
         char * x;
         z = new char;
         x = new char;
         while (start < end) {
               z = arg[start-1];
               strcat (z,x);
               start++;
         }
         return x;
    }
    int main ()
    {
        char * mostunleetarray[] = {"workyou"};
        char * value;
        value = gav (mostunleetarray,1,4);
        cout << value << "\n";
        system ("PAUSE");
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > z = new char;
    You don't need to allocate this at all, you just set it to point at some element of argv

    > x = new char;
    You need much more than one char to store a whole string, say for example
    x = new char[1000];
    Additionally, you need to make sure it's the empty string before you start strcat'ing to it
    *x = '\0';

    > strcat (z,x);
    Read the manual again - your parameters are the wrong way round.

    > value = gav (mostunleetarray,1,4);
    At least make the parameters consistent with the array you're passing.
    As it stands, you're off in no-mans land.
    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.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    humm....

    OK, thank you for the help. I changed what you said although i didnt quite understand this part--
    > value = gav (mostunleetarray,1,4);
    At least make the parameters consistent with the array you're passing.
    As it stands, you're off in no-mans land
    I supose it has somthing to do with the new problem....

    Code:
    #include <fstream.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    
    char * gav (char * arg[], int start, int end) {
         char * z;
         char * x;
         x = new char[1024];
         *x = '\0';
         while (start < end) {
               z = arg[start-1];
               strcat (x,z);
               start++;
         }
         return x;
    }
    int main ()
    {
        char * mostunleetarray[] = {"workyou"};
        char * value;
        value = gav (mostunleetarray,1,4);  //why does it not take the first 4 charactors?  
        cout << value << "\n";
        //it prints 'workyou' and some oher garble, why not just the charactors 1 to 4? ('work')
        system ("PAUSE");
        return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Because it takes the first 4 strings, not the first 4 characters
    Code:
    char *foo[] = { "this", "is", "an", "example", "list", "of", "strings" };
    value = gav (foo,1,4);
    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. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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