CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 1999
    Location
    Australia, Melbourne
    Posts
    10

    using an int [] array when the size is not known

    I need to use and declare a int [] array to pass to this function...

    something like

    int phPrinter[] = ???????

    and pass this phPrinter to this function... (the 2nd Parameter)

    OpenPrinter(String, int [], Printer_Defaults /* can be null */);

    The function initialises the int [] .

    Please does anybody know how to do this...

    I keep getting an error 87 (invalid parameters) when I try:

    int phPrinter[] = {0}; and pass that.

    the first param is just a string and the last (3rd) can be null so that leaves param 2... So how does one pass and unknown int array to a function. Class Vector doesn't do what I need to do or does it??

    Thanking you in advance.

    Chris


  2. #2
    Join Date
    May 1999
    Location
    Pune, MH, India.
    Posts
    453

    Re: using an int [] array when the size is not known

    I didn't get the problem completely. But if u want to initialize the variable phPrinter, u can try...

    phPrinter = new int[] { 0 };

    - UnicMan

  3. #3
    Guest

    Re: using an int [] array when the size is not known

    Hai,
    Let me tell u ur problem , u have a array , u wanted to pass it to a function f1 , and there u initialize it right

    see in main code
    {

    // if u declare an array , in java it is treated as an object so it need to initialized to something so make the reference as null
    then pass the reference to the function f1*/

    int[] a=null;

    f1(a);


    in function definition:
    void f1(int[] a)
    {
    a=new int[10];
    }

    All the best
    bye
    Asha



  4. #4
    Guest

    Re: using an int [] array when the size is not known

    Yes, a java.util.Vector will do what you need.

    Whenever you need an array of unknown dimensions, think Vector.


  5. #5
    Guest

    Re: using an int [] array when the size is not known

    The suggested approach
    int [] a = null;
    f1(x);
    won't work. In Java references are passed by copying their values,
    so f1() gets a !copy! of x pointing at nothing.
    f1() can assign the (copy of) x to anything with no impact on x itself.

    If you're stuck with an array, the option #1 is to make your array
    a method's return value:
    int x[] = f1(...);
    The option #2 assumes that you know in advance the # of elements,
    so you can do allocation upfront, e.g.:
    int [] a = new int[100];
    f1(x);
    The option #3 is to wrap your array into an object, e.g.:
    class x_wrapper {
    public int [] a;
    }
    and then

    x_wrapper a_w = new x_wrapper();
    f1(a_w);


    In both approaches #2 and #3 f1() will have a reference to smth allocated
    in the caller's space, so f1()'s modifications to the object refered by x
    will be visible by the caller.

    Of cource, using Vector will always work, but for the same reason it
    needs to be initialized before being passed to any method:

    Vector a = new Vector(...);
    f1(a);


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

Featured