Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   how to create adjacent arrays in memory? (http://www.go4expert.com/forums/create-adjacent-arrays-memory-t12520/)

sudeepta 28Jul2008 14:18

how to create adjacent arrays in memory?
 
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

xpi0t0s 29Jul2008 03:23

Re: how to create adjacent arrays in memory?
 
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?


All times are GMT +5.5. The time now is 17:40.