Thread: Help w/ Receiving a Char Array w/ a Function

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Help w/ Receiving a Char Array w/ a Function

    Hello All.

    Let me start by saying this is for a school assignment and I do not feel comfortable putting all of my code up so I will do my best to post a shortened version and explain what I am trying to do. In addition to this, I am not looking for someone to do my assignment for me. My question relates to a helper function that will essentially make the assignment easier for my implementation and not asking a directly "how do I do my homework" type question.

    Ok, with that disclaimer out of the way here is what I am trying to do.

    Essentially I am trying to receive back an array of char's in a certain order via a function. The reason I am calling a separate function in the first place is because I need to order the chars in a specific order depending on two other variables I am sending with the function.

    Here is a brief implementation:

    Code:
    /*helper function declaration*/
    void helpDist(int A, int B, char order[4]);
    
    
    int main()
    {
         char order[4] = { 'a', 'a', 'a', 'a' }; //*** do I need to initialize 
                                                //       the array sent???
         ...
         ...
         helpDist(4, 3, order [4]); // arbitrary numbers used
    
         // I now want the array order [4] filled in the certain
         // order as specified in helpDist
    }
    
    
    
    void helpDist(int A, int B, char order[4])
    {
         int w,x,y,z, temp, ord[4], i, j;
    
    
         w = A+B;
         x = A*B;
         y = A/B;
         z = A-B;
    
         // sort the list in ascending order
         for (i = 0; i < 4; i++)
         {
              for (j = 0; j < (3 - i); j++)
             {
                  if (ord[j] > ord[j + 1])
                 {
                      temp = ord[j];
                      ord[j] = ord[j + 1];
                     ord[j + 1] = temp;
                 }
             }
         }
    
    // Here is the part I question****
    // I want to fill the array of chars in a specific order
    // and "send the information" back
         for (i = 0; i < 4; i++)
         {
             if (ord[i] == w)
              {
                  order[i] = 'w';
              }
              else if (ord[i] == x)
              {
                  order[i] = 'x';
              }
              else if (ord[i] == y)
              {
                 order[i] = 'y';
             }
             else
             {
                 order[i] = 'z';
             }
         }
    
    
    }
    Do I need to this operation by reference? I have a feeling these values that are changed in the helper function are not reflected back in main where I have the calling function. Any insight on the matter would be greatly appreciated. I suppose I'm still not clear as to if the contents of an array need to be 'by ref' vs 'by val' since an array is essentially a pointer?

    Thanks ahead of time.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Arrays are always passed as a pointer (to the first element of the array).
    So anything you write into an array parameter is visible in the caller anyway - no extra effort required.

    The only obvious bit wrong was the call itself, which should be
    helpDist(4, 3, order);
    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.

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    2
    That appears to have done the trick, thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-22-2015, 03:59 AM
  2. Replies: 2
    Last Post: 09-25-2014, 04:03 AM
  3. Replies: 8
    Last Post: 04-09-2013, 07:06 PM
  4. receiving function value which returns array of strings
    By saeed144 in forum C Programming
    Replies: 3
    Last Post: 04-27-2008, 07:54 AM
  5. Receiving an array in a function
    By hern in forum C Programming
    Replies: 3
    Last Post: 04-21-2004, 08:51 AM