473,486 Members | 2,277 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

array of pointers



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
5 3582
please see the faq section on multi-dimensional array
Raj

Jul 23 '05 #2
AFAIR
T *p[n]; //array of T*
T (*p)[n]; //pointer to T[n]
Jul 23 '05 #3
* 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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
8451
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
20
2899
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
19
14486
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
204
12881
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
2110
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
5
3108
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
17
7210
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
2958
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
17
2292
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
33
7122
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d...
0
7105
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
6967
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
7132
Oralloy
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,...
0
7180
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...
1
6846
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...
0
5439
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,...
1
4870
isladogs
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...
0
3076
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...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.