Warning: Non-static method vBSEO_Storage::path() should not be called statically in ..../vbseo/includes/functions_vbseo_pre.php on line 387

Warning: Non-static method vBSEO_Storage::path() should not be called statically in ..../vbseo/includes/functions_vbseo_pre.php on line 387

Warning: Non-static method vBSEO_Storage::path() should not be called statically in ..../vbseo/includes/functions_vbseo_seo.php on line 337

Warning: Non-static method vBSEO_Storage::path() should not be called statically in ..../vbseo/includes/functions_vbseo_seo.php on line 339

Warning: Non-static method vBSEO_Storage::path() should not be called statically in ..../vbseo/includes/functions_vbseo_seo.php on line 341

Warning: Non-static method vBSEO_Storage::path() should not be called statically in ..../vbseo/includes/functions_vbseo_seo.php on line 343
Array arguments

Results 1 to 6 of 6

Thread: Array arguments

  1. #1
    Junior Member
    Join Date
    Feb 2007
    Posts
    25

    Array arguments

    I'm having a little trouble passing an array as an argument:

    Code:
    HWND buttons[8];
    I have 9 buttons on my GUI, and their handles are kept in the buttons array.

    I created each button (and got its window handle) by looping through the array in my WindProc procedure. Ex.

    Code:
    // in my WindProc
    int i;
    for (i = 0; i < 9; i++) {
    CreateButtons(buttons[i]);
    }
    and that works perfectly.

    My problem arises when I want to access all of the handles (loop through the array) inside a different function. Ex.

    Code:
    // in WindProc
    SomeFunction(HWND buttons);
    
    // definition of SomeFunction..
    BOOL SomeFunction(HWND buttons[8]) {
    int i;
    for (i = 0; i < 9; i++) {
    ShowWindow(buttons[i], SW_SHOW); // ShowWindow() is just an example
    }
    
    return TRUE;
    }
    The problem is that the last element of the array is not being accessed. I tried changing the loop to loop 999 times, and I tried ++i; neither worked. So I assume it&#39;s a problem in the actual passing..

    All suggestions are appreciated .

    EDIT: Pre-fix increment.. :

    Code:
    for (int i = 0; i < 9; ++i
    or
    Code:
     for (int i = 0; i < 8; i++)
    work. How would I do it using boolean logic, though?

    Code:
    buttons[9] = NULL;
    for (int i = 0; buttons[i]; i++)
    - post or pre didn&#39;t work

  2. #2
    VIP null's Avatar
    Join Date
    Apr 2006
    Posts
    1,796

    Re: Array arguments

    What do you have to do the looping for, because personally I would probably make a function and pass each array item individually.. Just looks nicer

    but if I read right and SomeFunction(HWND buttons); is your call, then you have to take out the HWND. If its a prototype, you need to make it an array.

    Also, Im not quite sure what you mean with the bottom part.. since you initialize buttons to 8 and you want to loop through length, make a define or global variable containing the size, so you can just update that?

    Also: If its not too long you could just post the full code to make it easier for us to find a problem.


  3. #3
    Junior Member
    Join Date
    Feb 2007
    Posts
    25

    Re: Array arguments

    I sorta fixed that problem, except it&#39;s still hardcoded. I&#39;ll try to make my new problem clearer:

    I&#39;m trying to use boolean logic for the condition in the loop.

    Code:
    // this isn&#39;t my actual code - example
    
    int numbers[9] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, NULL }; // initialize array
    void DoSomething(int numbers[9]); // declare function, argument being an array of 9 integers
    void DoSomethingWithArray(int number);
    
    int main() {
    DoSomething(numbers); // pass the array as an argument
    }
    
    void DoSomething(int numbers[9]) {
    int i;
    
    // loop through the array, passing each element as an argument to DoSomethingWithArray()
    /*
    there are multiple ways of doing this.. the first is hardcoding it:
    */
    
    for (i = 0; i < 9; ++i) {
    DoSomethingWithArray(numbers[i]); // this works
    }
    
    /*
    but nobody likes hardcoding.. so boolean logic would be the best way
    */
    
    for (i = 0; numbers[i]; i++) {
    DoSomethingWithArray(numbers[i]); // loop until numbers[i] isn&#39;t valid (check the 9th element of the array)
    }
    
    /*
    however, this doesn&#39;t work.. why?
    and don&#39;t say because I didn&#39;t define DoSomethingWithArray(); :P I meant why doesn&#39;t that ^ loop work.
    */
    }
    Any suggestions?

  4. #4
    VIP null's Avatar
    Join Date
    Apr 2006
    Posts
    1,796

    Re: Array arguments

    er, see below:
    PHP Code:
    for (08; ++i) {
    DoSomethingWithArray(i); // why numbers[i]? since it will always be the number i is, why not just i?
    }

    /*
    but nobody likes hardcoding.. so boolean logic would be the best way
    */

    for (0numbers[i]; i++) { //this wont work because you need some sort of check! you should just use the other loop
    DoSomethingWithArray(numbers[i]); // loop until numbers[i] isn't valid (check the 9th element of the array)
    }

    /*
    however, this doesn't work.. why?
    and don't say because I didn't define DoSomethingWithArray(); :P I meant why doesn't that ^ loop work.
    */ 
    More specifically, your code is doing exactly what you could be doing with a simple for loop without the numbers array.


  5. #5
    Junior Member
    Join Date
    Feb 2007
    Posts
    25

    Re: Array arguments

    Quote Originally Posted by null
    er, see below:
    for (i = 0; i < 8; ++i) {
    DoSomethingWithArray(i); // why numbers[i]? since it will always be the number i is, why not just i?
    }
    The number array was just an example. Let&#39;s say I had an array of window handles..

    for (i = 0; numbers[i]; i++) { //this wont work because you need some sort of check! you should just use the other loop
    DoSomethingWithArray(numbers[i]); // loop until numbers[i] isn&#39;t valid (check the 9th element of the array)
    }
    More specifically, your code is doing exactly what you could be doing with a simple for loop without the numbers array.
    I do have a have a check! The check is if numbers(i) is a valid value: since the 9th element is NULL, when it reaches that, boolean logic dictates that numbers(i) is invalid and therefore the condition is false. (The parentheses replace the &#91;] in that paragraph because is BBcode for italics :P ).

    But it won&#39;t even let me set the 9th element!

    I don&#39;t want to use the first way because that would be hardcoding, and hardcoding is very bad practice :-\

  6. #6
    VIP null's Avatar
    Join Date
    Apr 2006
    Posts
    1,796

    Re: Array arguments

    Code:
    int size = 8; //or however big
    HWND array[size]; //whatever array define here
    Code:
    void DoSomething(HWND * item)
    {
      //whatever you need here to work with item
    }
    Code:
    for (int i=0; i<size; i++)
    {
      DoSomething(&array[i]);
    }
    I think what may be happening is that you are trying to use the array, but then when you change things in the function the buttons arent changing as you want them in the array? If this is the case, using pointers will fix your problem


Similar Threads

  1. Remote inject module from byte array
    By ntKid in forum C/C++
    Replies: 0
    Last Post: January 5th, 2012, 08:18
  2. Replies: 2
    Last Post: October 1st, 2010, 09:39
  3. Replies: 0
    Last Post: September 12th, 2010, 14:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •