how to create adjacent arrays in memory?

Discussion in 'C' started by sudeepta, Jul 28, 2008.

  1. sudeepta

    sudeepta New Member

    Joined:
    Jul 28, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i'm new to programming. I wanted to create two integer arrays which are adjacent in memory. i had written the following code, but i'm getting segmentation fault when i run it.

    Code:
    #include<iostream>
    using namespace std;
    
    void printarray(int* a,int size)
    {
            for(int i=0;i<size;i++)
            cout<<a[i]<<endl;
    }
    
    int main()
    {
            int a[3]={0,1,2,}; //the first array
            int* pa;
            pa = &(a[2]);
            pa++;
             int* b = pa; //the second array
    
    
            for(int i=0;i<3;i++)
            {
                    b[i]=i+2;
             }
            printarray(a,3);
            printarray(b,3);
    
    }
    any help would be greatly appreciated..thanks
     
    Last edited by a moderator: Jul 28, 2008
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Why do you want them to be adjacent in memory, what problem do you think this will solve?
    If you want them adjacent, why not just allocate one array that will hold the items for both?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice