Thread: Passing Arrays of Unknown Size

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    40

    Passing Arrays of Unknown Size

    Hey guys...

    I'm looping through a file and creating arrays of unknown size depending on the data in the file. I'm having trouble passing the array to another function for more processing. I did a google but each method I tried just seemed to make the compiler go crazy with error messages.

    Is there a quick an easy way I can do this

    If I just try to pass it like was an Int I get a "error: ‘ex_data’ undeclared (first use in this function)"

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You'll need to show some code that illustrates your problems if you want any help.

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    The code is kind of long but I can try to give a look... Im reading opcodes and operands from a file.

    So at the end of this if tree I have created an array "ex_data[i]" with a size dependant on the opcoade read from the file.

    Code:
      if (opcode == 0) {
              f_dx(&numbers, operand);
            }
            else if (opcode == 1) {
              f_dy(d, &numbers, operand);
            }
            else if (opcode == 2) {
              f_dt(d, &numbers, operand);
            }
            else if(opcode == 3){
              int hold;
              //dectect extended dx dy dt
              if (  ((operand & 0x30) >> 4) != 0 ) {
                hold = ((operand & 0x30) >> 4);
                //convert 3 to 4
                if (hold == 3) hold = 4;
    
    
                //printf("%d\n", hold);
                unsigned char ex_data[hold];
    
    
                //get bytes
                for (int i = 0; i < hold; i++) {
                  c = fgetc(in);
                  ex_data[i] = c;
                  printf("%02x\n", ex_data[i]);
                }
              }
    
    
    
    
              ex_handler(operand, &numbers);
            }
    I need to pass this array to the function ex_handeler at the bottom so it can be processed. I havent written the code yet... I have just done a little house keeping.

    here is the ex_handeler function:

    Code:
    void ex_handler(int op, Numbers *numbers){
    
    
      //check for pen
      if (  ((op & 0x30) >> 4) == 0 && ((op & 0x0c) >> 2) == 0){
        //printf("Pen Working\n");
        f_pen(numbers);
      }
    
      //printf("ex workng\n");
    
    
    }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    How are operand and numbers defined, and what exactly is a Numbers?

    Look at this snippet:
    Code:
            if (hold == 3) hold = 4;
     
     
              //printf("%d\n", hold);
              unsigned char ex_data[hold];
    Do you realize that that last line is creating a VLA?


    Jim

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    So what I'm doing for the assignment is pulling out a operation and values from each byte of the file and then passing them to a few SDL draw factions that I have been given.

    Numbers is a struct holding information that needs to be passed to the draw functions. Right now this last bit of code at the bottom of the IF tree I'm working on doesn't interact with Numbers.

    So this part:

    Code:
    else if(opcode == 3){
              int hold;
              //dectect extended dx dy dt
              if (  ((operand & 0x30) >> 4) != 0 ) {
                hold = ((operand & 0x30) >> 4);
                //convert 3 to 4
                if (hold == 3) hold = 4;
    
    
                //printf("%d\n", hold);
                unsigned char ex_data[hold];
    
    
                //get bytes
                for (int i = 0; i < hold; i++) {
                  c = fgetc(in);
                  ex_data[i] = c;
                  printf("%02x\n", ex_data[i]);
                }
              }
    
    
    
    
              ex_handler(operand, &numbers);
            }
    I can give you a more detailed explanation of what I'm trying to do if you want. It might be hard for to try explain it though. I cant link you to the assignment brief unfortunately because you have to be logged in. I could copy and past it but its a few pages long.

    I have no idea what a VLA is.

    I just want to have an array "ex_data [ ]" of unknown size that I can pass around to my functions. I want to pass it to
    ex_handler() at the bottom.
    Last edited by Xecutive; 11-23-2016 at 02:47 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I just want to have an array "ex_data [ ]" of unknown size that I can pass around to my functions.
    Since you don't know what a VLA is you should be using dynamic memory allocation with malloc() and free().

    I want to pass it to ex_handler() at the bottom.
    But ex_handler() seems to expect an int and a pointer to a Number, ex_data[] is an array of char not a Number.

    Jim

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Oh yeah I didnt know how to define the array in the function so I just left it out. Could you give me a short example please? Just showing how I would allocate the space and then pass the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unknown size array
    By archriku in forum C Programming
    Replies: 14
    Last Post: 05-07-2009, 11:29 PM
  2. Allocating an array of unknown size?
    By scarlet00014 in forum C Programming
    Replies: 2
    Last Post: 09-27-2008, 09:53 AM
  3. array of unknown size
    By richdb in forum C Programming
    Replies: 7
    Last Post: 02-25-2006, 11:48 AM
  4. A vector or an array of unknown size
    By strickey in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2005, 09:11 AM
  5. How to make unknown size arrays?
    By KneeLess in forum C Programming
    Replies: 6
    Last Post: 09-08-2003, 12:08 PM

Tags for this Thread