array of pointers

H

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];

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 = NULL;

and to allocate new somewhere later

for i=0; i=20 i++
ydata = 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] = double(rev_data);


At it just crashes.


Thaks for your help

Kamran
 
A

Alf P. Steinbach

* 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 = 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 = 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] = double(rev_data);


At it just crashes.


Compare that loop to the previous ones.
 
O

Old Wolf

Alf said:
* 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 = 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 = 0;
After allocating new I try this:

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

At it just crashes.


Compare that loop to the previous ones.


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

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

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

yinquan

Henrietta Denoue said:
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 = NULL;

and to allocate new somewhere later

for i=0; i=20 i++
ydata = 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] = double(rev_data);


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=NULL;
system("pause");
return 0;
}

In the "C++ Primer" ,C++ can use NULL or 0 to point.Both thing are right.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top