Thread: simple question in arrays

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    38

    simple question in arrays

    What does it mean when passing an array in this way :

    key[16];
    j+=0;
    k+=0;

    getArray(key + j, key + j + k, k, k);


    is that shifting or something similar ?

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Try to print the first element of the 1st argument and of the second argument inside the function. Before calling the function initialize the key array to something like 0,1,2,3,...,16 or something like that. Then remember how pointers and arrays are connected.

    And do not forget, always post your code in code tags!!!!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, it is not "shifting".

    Assuming array_name is an array of N elements, and i is an integral value,such as
    Code:
        float key[N];   /* float type picked at random */
        int i;
    then the notation "array_name + i" is a pointer with the value equal to "&array_name[i]".

    Any attempt to use that pointer (e.g. inside your function) will yield undefined behaviour unless i is a valid index (for an array of N elements, i is only a valid index if it is between 0 and N-1).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by abood1190 View Post
    What does it mean when passing an array in this way :
    getArray(key + j, key + j + k, k, k);
    It's not passing an array, it's passing two pointers to two locations in the array and then passing the same integer twice. As mentioned above, it's the same as:

    getArray(&key[j], &key[j+k], k, k);

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    your comments are very helpful guyz

    I notice from the output that it changes the result of some elements

    lets say this code :

    Code:
    key[16];
    j+=0;
    k+=0;
    
    getArray(key + j, key + j + k, k, k);



    the + will change 1 value of the array key[] from the right when j is 1 to a weird value looks like an address and when it is -1 that will change 1 value from the left of the array !! why is that
    behavior?
    Last edited by abood1190; 05-12-2013 at 09:17 AM.

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Please provide a minimal complete program, its ouput, and your question.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    Quote Originally Posted by CodeMonkey View Post
    Please provide a minimal complete program, its ouput, and your question.

    Code :


    Code:
    #include <conio.h>
    #include <stdio.h>
    
    
    
    
    void getArray( int key[] ){
       int i;  
      for ( i=0 ; i<6 ; i++){
          printf(" %d ",key[i]);}     
         
         }
    
    
    
    
    main()
    {
          
       int i;      
    int array[]={1,2,3,4,5,6};
    getArray(array+1);
     
        getch();
    }

    Output :

    2 3 4 5 6 2293616

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are accessing the array out of bounds because getArray accesses key[5], which corresponds to array[6], which does not exist.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    38
    Quote Originally Posted by laserlight View Post
    You are accessing the array out of bounds because getArray accesses key[5], which corresponds to array[6], which does not exist.
    Sorry, i didn't understand

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Compile and run this program:
    Code:
    #include <stdio.h>
    
    void printArray(int numbers[], int size)
    {
        int i;
        for (i = 0; i < size; i++)
        {
            printf("%d ", numbers[i]);
        }
    }
    
    int main(void)
    {
        int array[] = {1, 2, 3, 4, 5, 6, 7};
        printArray(array + 1, 6);
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++:Simple program using 2d arrays?
    By rvbplayer123 in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2012, 03:25 PM
  2. simple question for a beginner with arrays
    By Student12321 in forum C Programming
    Replies: 5
    Last Post: 04-08-2012, 12:10 PM
  3. Simple arrays
    By DerrickakaDRoC in forum C Programming
    Replies: 12
    Last Post: 12-12-2010, 10:57 PM
  4. Replies: 2
    Last Post: 01-30-2009, 05:58 AM
  5. Simple question about arrays
    By tima in forum C Programming
    Replies: 3
    Last Post: 05-21-2007, 09:24 AM