returning char array from a function to main

This is a discussion on returning char array from a function to main within the C++ Programming forums, part of the General Programming Boards category; ive been trying to return a sorted array elements but its only returning the half of the array. any ideas ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    returning char array from a function to main

    ive been trying to return a sorted array elements but its only returning the half of the array. any ideas why?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,538
    Post your code.

    Anything else would be wild speculation on our part.
    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
    Nov 2012
    Posts
    22
    bacically im trying to shift chars in an array inside a function..

    Code:
    char *shifter(int numOfShifts)
    {
    char arr[] = "abcdefghijklmnopqrstuvwxyz";
    ...
    shifting
       ...
     
    cout << arr << endl;
    return arr;
    If I call this function.. Im passing in a value(int) of how many shifts i want to do.
    in return arr, its only returning halfo of the alphabets. but when printed in cout << arr << endl; the it prints out the whole alphabet

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,538
    You're returning a pointer to a local variable!
    So when the variable goes out of scope, the pointer is then pointing at non-existant data (well data you don't own anyway).

    It's C++, so perhaps start with
    std::string shifter(int numOfShifts)

    There are several ways to solve it in a 'C' way, but none are as simple and elegant as the C++ std::string approach.
    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.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    34
    i have similar question
    i have
    Code:
    ???make_data_list_from_files()
    {
        char g;
        int n=count_member();
        user_data pointer[n];//user_data is a class
         std::fstream file1;
         std::string map="%s/%s.dat";
         std::string  file1_str= string_print(map,R.c_str(),"Fname");
         file1.open (file1_str.c_str(), std::fstream::in |  std::fstream::app);
           std::string tmp;
         int i=0,p_num=0;
         while(!file1.eof())
         {
               g=file1.get();
               tmp.resize(i+1);
               tmp[i]=g;
               i++;
            if(g=='|')
            {
                 tmp.resize(i-1);
                 pointer[p_num].F_name(tmp);
                  i=0;
                 tmp.resize(0);
                 p_num++;
            }
         }
         
                
    return what??
    }
    now how can i return this array for using in another function??
    Last edited by king_zart; 12-09-2012 at 12:35 PM.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    Use an std::vector, and return that.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  2. function returning char array help
    By te5la in forum C++ Programming
    Replies: 10
    Last Post: 09-18-2008, 11:13 AM
  3. returning a char* array
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2006, 04:13 PM
  4. Returning arrays from function to main
    By musayume in forum C Programming
    Replies: 3
    Last Post: 05-12-2003, 04:33 AM
  5. HELP: Function returning char *
    By FaridEsna in forum C Programming
    Replies: 9
    Last Post: 10-17-2002, 02:46 PM

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