Jump to content

Array of pointers to int arrays

- - - - -

  • Please log in to reply
27 replies to this topic

#1
DMK741

DMK741

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I need to create an array of pointers that point to int arrays. Can someone clarify the proper syntax for me?

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,



int array[100];

int **pointerToArray = & array;


I hope this helps!

Munir

#3
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I think that this might be what your after.
int i[1000][1000]; // Creates a 2-dimensional array
But it sounds as if you don't want to have the array be contiguous in memory. Meaning if the array's dimensions were 1000x1000, you don't want a single chunk of 1,000,000 objects in your RAM cause it might not fit too great. If this is the case, take a look at the following code (if it isn't, ignore the following code).
int * array[1000]; // 1000 is the width of the array
for (int i = 0; i < 1000; ++i)
    array[i] = new int[1000]; // 1000 is also the height of the array

// Executed at some point..
for (int i = 0; i < 1000; ++i)
    delete [] array[i];


#4
DMK741

DMK741

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Actually, what I'm trying to do is create a 3D array on the heap. and in order to do so, I have to mess around with pointers. Unfortunately, this got pretty confusing.

#5
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Then perhaps this is closer to what your after.
int a[1000][1000][1000]; // 3-Dimensional array
And the non-contiguous version..
int *** a = new int ** [1000];
for (int i = 0; i < 1000; ++i) a[i] = new int*[1000];
for (int i = 0; i < 1000; ++i)
    for (int j = 0;  j < 1000; ++j)
        a[i][j] = new int[1000];
(You should of course be using vectors, but you did explicitly say array so I assumed you were talking about true arrays)

#6
DMK741

DMK741

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
Why are vectors any better to use?

#7
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Vectors provide automation of various common tasks done with arrays with negligible overhead. A vector will also automatically deallocate itself which is immensely useful.
I will go ahead and say, always use vectors and never use arrays. There will be exceptions to this of course, but if you are a beginning programmer you don't have to worry about such exceptions.

Reference on Vectors

#8
DMK741

DMK741

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
I see... thank you for showing me this. However, it is possible that I may end up working on something with very limited space. Also, I'd like to learn to do it the hard way before I do it the easy way. I'll play around with these arrays some more, and if I need help I'll post back.

#9
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Alright, however, using arrays over vectors does not save space. A vector will store the length of the array, which means it is storing sizeof(size_t) extra bytes of space, however, you'd need this information almost certainly anyways. Also, the size of an int which is usually 4 bytes, is extremely tiny compared to your array. Learning is fine, but if your not exploring how to work with arrays, use vectors, it is always the better choice. (Again, exceptions: yes, worry: no).

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
FYI if you're working on something with "very limited space" you're probably going to want to use straight C. Since vectors are a C++ thing, you might have to use regular arrays anyway.
sudo rm -rf /

#11
DMK741

DMK741

    Learning Programmer

  • Members
  • PipPipPip
  • 32 posts
byte*** bob = new byte**[4];

bob[0] = new byte*[5];

bob[0][0] = new byte[5];



I get an error over the first bracket here: "expected an expression". Any idea what I'm doing wrong?

bob[0][0] = {0, 0, 0, 0, 0};

Attached File  Untitled.png   3.36K   9 downloads

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
byte*** bob = new byte**[4];

bob[0] = new byte*[5];

bob[0][0] = new byte[5];  [COLOR="blue"]/* why are you doing this? */[/COLOR]


[COLOR="blue"]/* Illegal unless you're doing it all at once at initialization */[/COLOR]

bob[0][0] = {0, 0, 0, 0, 0};

sudo rm -rf /




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users