473,698 Members | 2,411 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

two dimensional array

im trying to declare a two dimensional array using pointers. but it is not
working, any ideas?
Jul 23 '05 #1
20 2687
* Someonekicked:
im trying to declare a two dimensional array using pointers. but it is not
working, any ideas?

Use std::vector.

For concrete examples, see chapter 1.7 of
<url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01.ht ml>.

A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
"Someonekic ked" writes:
im trying to declare a two dimensional array using pointers. but it is not
working, any ideas?

Like this:

#include <iostream>
using namespace std;

void test2(int b[][5])
cout << b[2][3] << endl;
//=============== =====
int main()
int(*a)[5] = new int[3][5];
a[2][3] = 2048;
Jul 23 '05 #3

"Someonekic ked" <so***********@ comcast.net> wrote in message news:Vs******** ************@co mcast.com...
| im trying to declare a two dimensional array using pointers. but it is not
| working, any ideas?

Programming is communicating in the first place.
1) Does the matrix know the rows and columns at compile time or run time only?
2) Why isn't working? Where is the code?
3) How did you express the Matrix? As a single array of row*col size, or as an array holding arrays?

See the C++ faq lite (google it) for your first hints.
Jul 23 '05 #4

Someonekicked wrote:
im trying to declare a two dimensional array using pointers. but it is not working, any ideas?

There are many ways to create a two dimensional array in C++.

You can use a vector of vectors
std::vector<std ::vector<int> > My2dOfInt;

You can also use a custom class, or you can use a pointers of pointers.

Check out the following link for examples, and for different methods.

Jul 23 '05 #5
I would say, the simplest way would be

int **p ;
p = new int *[ROWS] ;
for( int i = 0 ; i < ROWS ; i++ )
p[i] = new int[COLUMNS];

But everyone seems to suggest vectors...

Jul 23 '05 #6

Angad wrote:
I would say, the simplest way would be

int **p ;
p = new int *[ROWS] ;
for( int i = 0 ; i < ROWS ; i++ )
p[i] = new int[COLUMNS];

But everyone seems to suggest vectors...

You're missing the code to free the above allocated data.
Using **p (pointer of pointers) you need code for allocating the data,
code for keeping track of the sizes of the rows and columns and code to
deallocated the data.

So using **p is not the simplest approach.
It's far easier to use a vector that wraps all the logic behind a safe
Your above code can be done in one line of code by using vectors.
vector<vector<i nt> > My2DArr(ROWS, std::vector<int >(COLUMNS));

IMHO, this one line of code is far simpler then the pointer of pointer
code, and the required tracking code, plus the required deallocation
code needed for the pointer of pointer method.

Jul 23 '05 #7
| Your above code can be done in one line of code by using vectors.
| vector<vector<i nt> > My2DArr(ROWS, std::vector<int >(COLUMNS));

std::valarray for numbers.

Or single array acting as a Matrix:
int* Matrix = new int[ROWS*COLS];

But I think it's a nice excercise to do it the ** way :)
Jul 23 '05 #8
Someonekicked wrote:
I'm trying to declare a two dimensional array using pointers.

Take a look at the FAQ


[16.16] How do I allocate multidimensiona l arrays using new?
Jul 23 '05 #9
Yes, I agree that vectors simplify the matter a lot by reducing our
coding, but I must say that they add to a lot of unnecessary code to
the executable code. Compile time increases manyfold (I'm talking from
a personal experience). I'd say, use vectors only when basic data types
cannot be used....

As for the freeing of the memory, the code is:

for( int i = 0 ; i < ROWS ; i++ )
delete [] p[i] ;
delete [] p ;

This code will definitely be faster than using vectors... (please
comment if I'm wrong)

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray = (data_type**)malloc(widht*height*sizeof(data_type)+ height* sizeof(data_type*)); //allocate individual addresses for row pointers. Now that I am moving to C++,am looking for something by which I can
by: Ruben | last post by:
I'm trying to pass an array of string to a function without knowing how many strings I have beforehand. I've defined one functions as char * insert(char table,int cols, char values); out of deperation because when I defined it as char * insert(char table,int cols, char values)
by: rguti | last post by:
Hi, How do I create a two dimensional array? I have created a one dimensional doing this: Dim laFields As ArrayList = New ArrayList How about to do a 2 dimensional?
by: Peter Olcott | last post by:
I need to know how to get the solution mentioned below to work. The solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below: > http://groups.google.com/group/comp.lang.c++/msg/db577c43260a5310?hl > >Another way is to create a one dimensional array and handle the >indexing yourself (index = row * row_size + col). This is readily >implemented in template classes that can create dynamically allocated >multi-dimensional...
by: Diffident | last post by:
Hello All, I have a 2-dimensional array that I am storing as a session variable. I have no idea on how I can cast the session variable back to 2-dimensional array. Any pointers? Reference code below... Array declaration: DateTime DateRangesForDataLists = new DateTime;
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest that it will be evaluated like "x", just as "x" is evaluated like "x" right now.
by: fniles | last post by:
I need to store information in a 2 dimensional array. I understand ArrayList only works for a single dimensional array, is that correct ? So, I use the 2 dimensional array like in VB6. I pass the array into a subroutine, and inside it I "Redim Preserve" the array to increase the number of item in the array. I got the error "Redim statement requires an array", but arr is an array. HOw can I fix this problem ? Or, how can I do ArrayList for...
by: per9000 | last post by:
Hi all, I have a two-dimensional array of data, f.x int's. We can imagine that the array is "really large". Now I want the data in it and store this in a one-dimensional array. The obvious way to do this is a nested for-loop - but we all know O(n^2) is bad. So I am looking for something like ArrayList.ToArray(), or Matlabs A(:). C#
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does anyone else know how to make a template for making two dimensional arrays from std::vectors ??? I want to use normal Array Syntax.
by: nelly0 | last post by:
developing a program that will manipulate noise levels (measured in decibels) that is collected by car manufacturers. These noise levels are produced at seven different speeds by a maximum of six different models of cars that are produced by the car manufacturer. Task 1 Step 1: Create a directory called Assignment02 and create the files of steps 2, 3 and 4 in this directory as well as your project file. Step 2: Create a file called...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.