473,383 Members | 1,735 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,383 software developers and data experts.

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 3573
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
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
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
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
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
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
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
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
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
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
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.