473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

I can't seem to get the syntax correct for a pointer to pointer to const length array

For a runtime calculated dimension of size NX the following syntax is correct.

Expand|Select|Wrap|Line Numbers
  1.   double (*var)[2] = new double[NX][2];
  2.  
But what I also want is a runtime dimensioned array of pointers to these guys but I can not find the proper syntax. Something akin to the following (which is wrong).

Expand|Select|Wrap|Line Numbers
  1.   double *(*var)[2] = new double*[NX][2];
  2.   for(i=0; i<NX; ++i)
  3.     {
  4.     var[i] = new double[NY][2]; // NY != NX
  5.     }
  6.  
What is the correct syntax?
Oct 15 '10 #1
1 1265
weaknessforcats
9,208 Expert Mod 8TB
This dynamically allocates a 2D array.

var is a pointer to an array of 2 doubles.

Expand|Select|Wrap|Line Numbers
  1.     double (*var)[2] = new double[NX][2]; 
Now to allocate an array of pointers to arrays of 2 doubles you code:

Expand|Select|Wrap|Line Numbers
  1.     double* (*ptr)[2] = new double* [NX][2];
Now ptr is an array of pointers that contain addresses of arrays if 2 doubles.

Finally, you assign to the ptr array by:

Expand|Select|Wrap|Line Numbers
  1. *(ptr[0]) = var[0];
since ptr[0] is an address of a pointer it must be that
*(ptr[0]) is element 0 of the ptr array. Therefore, you assign var[0] (which is the address of an array of 2 double) to *(ptr[0]) which is also the address of an array of 2 double.
Oct 17 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: cppaddict | last post by:
I can't figure out why this code won't work, or how to fix it: list<MyClass> l; int main() { for (int i=0;i<5;i++) { MyClass* myClass = new MyClass; myClass -> someInitMethod();...
5
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
31
by: RS | last post by:
Hi, Looking to see if the following construct is valid: typedef struct { int foo; char bar; } foobar; Basically, the idea is to have the structure above point to a message buffer that has...
22
by: Neo | last post by:
Hi Folks, #include<stdio.h> int main() { int (*p); int arr; int i;
12
by: junky_fellow | last post by:
How can I declare a function that returns a pointer to one dimensional array ?
8
by: lovecreatesbeauty | last post by:
Hello experts, I have seen following the code snippet given by Marc Boyer (with slight changes by me for a better format), and have doubts on it. I am so grateful if you can give me your kindly...
7
by: Neil | last post by:
What I am doing wrong This works batPointer = adaptors.adaptor->batData; adaptors.batteries = batPointer->battery; where: batData is a pointer to a struct batPointer is a pointer to a...
6
by: babakandme | last post by:
Hi to every body...:D I'm a novice C++ programmer & I've a question, I have the ClassA & in it's constructor, I instantiate ClassB, and I want send "this" pointer """pointer to ClassA""" to the...
6
by: ThomasW | last post by:
Hi all, I'm not a very experienced C programmer and probably miss something very basic, so please forgive me. I modified a function that tests whether a point lies within a polygon (from the...
27
by: aravind | last post by:
Hi, I need to develop a menu system for a project of mine. The menu will be displayed on a character LCD display driven by ARM7 Microcontroller. For this purpose i wish to construct a structure in...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...

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.