473,770 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ 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 3613
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*******@netc alcul.fr> ????
news:d2******** **@dolly.uninet t.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
8484
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
2976
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) struct, I am storing an array of 27 pointers and a void pointer that can point to anything. typedef struct trieNode { struct trieNode *children; // The children nodes void *obj; // The object stored } TrieNode;
19
14519
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
13101
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 = {0,1,2,4,9};
2
2131
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 have an array of structs containing data which I'd like to output ordered based on the value of a single member. I was wondering if there is a relatively simple way of doing this without actually modifying the structure of the array? I had a...
5
3130
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
7255
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 to show the array data to the end user. Can I do that? How?
2
2985
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 standard dynamic address book program. Adding, and listing work just fine. However, deleting, editing and viewing relies on member function retAddress. This function returns an array of pointers that are pointing to created objects. In action, all it...
17
2326
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 same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of the notion of
33
7185
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 array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.