By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

array of pointers

P: n/a


Hi

A question about array of pointers.

Is the following an 21 element array, each a pointer
or is this a pointer to ONE array (strange as it sounds):

double* ydata[21];

And supposing this to be 21 pointers how do I initialize
them to point to null ? Is this wrong:

for i=0; i=20 i++
ydata[i] = NULL;

and to allocate new somewhere later

for i=0; i=20 i++
ydata[i] = new double[1000];
This used to work in C but doesn't with C++ !
After allocating new I try this:
for (int i = 0; i < 1000; i++) {
ydata[0][i] = double(rev_data[i]);
At it just crashes.
Thaks for your help

Kamran

Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
please see the faq section on multi-dimensional array
Raj

Jul 23 '05 #2

P: n/a
AFAIR
T *p[n]; //array of T*
T (*p)[n]; //pointer to T[n]
Jul 23 '05 #3

P: n/a
* Henrietta Denoue:


Hi

A question about array of pointers.

Is the following an 21 element array, each a pointer
or is this a pointer to ONE array (strange as it sounds)

double* ydata[21];
A 21 element array, each element a pointer.

And supposing this to be 21 pointers how do I initialize
them to point to null ? Is this wrong:

for i=0; i=20 i++
ydata[i] = NULL;

Yes, that is wrong, both syntactically (it's not C++) and in intent.

Easiest way is

double* ydata[21] = {};

and to allocate new somewhere later

for i=0; i=20 i++
ydata[i] = new double[1000];
This used to work in C but doesn't with C++ !
That has never worked in C, it's neither C nor C++ syntax.

After allocating new I try this:
for (int i = 0; i < 1000; i++) {
ydata[0][i] = double(rev_data[i]);
At it just crashes.


Compare that loop to the previous ones.

--
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 #4

P: n/a
Alf P. Steinbach wrote:
* Henrietta Denoue:

double* ydata[21];

And supposing this to be 21 pointers how do I initialize
them to point to null ? Is this wrong:

for i=0; i=20 i++
ydata[i] = NULL;

Yes, that is wrong, both syntactically (it's not C++) and in intent.


I think the intent is correct (if not optimal), this would work too:

for (int i = 0; i != 21; ++i)
ydata[i] = 0;
After allocating new I try this:

for (int i = 0; i < 1000; i++) {
ydata[0][i] = double(rev_data[i]);

At it just crashes.


Compare that loop to the previous ones.


If it crashes then there must be something wrong with rev_data[i],
assuming the OP fixed his syntax errors:

for (int i = 0; i != 21; ++i)
ydata[i] = new double[1000];

for (int i = 0; i != 1000; ++i)
ydata[0][i] = rev_data[i];

Jul 23 '05 #5

P: n/a

"Henrietta Denoue" <he*******@netcalcul.fr> ????
news:d2**********@dolly.uninett.no...


Hi

A question about array of pointers.

Is the following an 21 element array, each a pointer
or is this a pointer to ONE array (strange as it sounds):

double* ydata[21];

And supposing this to be 21 pointers how do I initialize
them to point to null ? Is this wrong:

for i=0; i=20 i++
ydata[i] = NULL;

and to allocate new somewhere later

for i=0; i=20 i++
ydata[i] = new double[1000];
This used to work in C but doesn't with C++ !
After allocating new I try this:
for (int i = 0; i < 1000; i++) {
ydata[0][i] = double(rev_data[i]);
At it just crashes.
Thaks for your help

Kamran


This Programe isn't wrong.I saw it and try it in visual c++.net using std
c++. It pass.
I write my programe down :

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
double * t[21];
for (int i=0; i<21; ++i)
t[i]=NULL;
system("pause");
return 0;
}

In the "C++ Primer" ,C++ can use NULL or 0 to point.Both thing are right.
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.