how to create adjacent arrays in memory?

sudeepta's Avatar, Join Date: Jul 2008
Newbie Member
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 shabbir; 28Jul2008 at 14:51.. Reason: Code block
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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?