Allocating data arrays in C++


 
Thread Tools Search this Thread
Top Forums Programming Allocating data arrays in C++
# 1  
Old 10-30-2010
Allocating data arrays in C++

lets say that I have a two dimensional array:
Code:
char myarray[10][10];

and my process of taking in values from an input file is this:
Code:
for(i=0;x<2;i++)  // 2 is the amount of rows in the array
{ 
input>>myarray[x];
}

now when I display my array my output is this:
why
hello

To make this work dynamically I am trying to go about doing it this way:

Code:
char* a = new char[max];
for(i=0;x<2;i++)  // 2 is the amount of rows in the array
{ 
input>>myarray[x];
}

now when I try to output my input, I go about doing it this way:
Code:
for(i=0;x<2;i++)
{
cout<<myarray[x];
}

I am currently having trouble with pointers...
# 2  
Old 10-30-2010
Your previously two-dimensional array has become a one-dimensional array, since a "char *" just points to one list of characters. And in dereferencing it, you're telling cin to read in a single character.

Try
Code:
char **strings=new char *[2];

for(n=0; n<2; n++)
{
       strings[n]=new char[max];
       cin >> strings[n];
}

# 3  
Old 10-30-2010
Quote:
Originally Posted by Corona688
Your previously two-dimensional array has become a one-dimensional array, since a "char *" just points to one list of characters. And in dereferencing it, you're telling cin to read in a single character.

Try
Code:
char **strings=new char *[2];

for(n=0; n<2; n++)
{
       strings[n]=new char[max];
       cin >> strings[n];
}

ohhhh so you need 2 **'s to reference a double dimensional array?
# 4  
Old 10-30-2010
Quote:
Originally Posted by puttster
ohhhh so you need 2 **'s to reference a double dimensional array?
Well, this isn't precisely a two-dimensional array, either. It's a one-dimensional array, but it's an array of pointers. So you can allocate memory and keep a pointer to that memory in each slot. A single 'char *' can't do that since it holds characters, not pointers.
# 5  
Old 10-30-2010
What I am having trouble with right now is that I cannot take multiple lines of single words and dynamically take them from the input file and display it.

However, I can do the easier part which is take the averages of the data ( which is stored in a single dimensional INT array ) and dynamically allocate that data.

So to sum things up, I am having trouble working with 2d arrays ( int and char ).
While dynamically allocating the input ( I understand 2d arrays but not the dynamically allocating part ).

Further explanation.

error C2440: '=' : cannot convert from 'char (*)[12]' to 'PointerToChar'

UPDATE

I am now only stuck on dynamically allocating the 2 words on the right of my data.

Last edited by puttster; 10-30-2010 at 07:10 PM..
# 6  
Old 10-30-2010
Please post your code.
# 7  
Old 10-30-2010
Code:
typedef char* characterptr;
characterptr *stuff=new characterptr;
for(x=0; x < 2; x++) 
{ 
	 
	input >>stuff[x];}

Is that correct so far?

Then to display it,
Code:
for(y=0;y<lengthofvar;y++){
	cout<<stuff[x];
	}


I don't get errors either, just a crash.

---------- Post updated at 07:01 PM ---------- Previous update was at 06:21 PM ----------

taking a 2 hour break, but ill be back to see if you answered my question!

Last edited by puttster; 10-30-2010 at 07:38 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Gpg: out of memory while allocating 8192 bytes

We are receiving the below error message when trying to encrypt or decrypt a file on AIX server : gpg: out of memory while allocating 8192 bytes gpg process was working for years on the server until the day we started to see this. This same gpg encryption is working on an other AIX server... (17 Replies)
Discussion started by: sradithya
17 Replies

2. UNIX for Dummies Questions & Answers

allocating space for samba users

I have installed samba by cmd yup install samba -a and configured my samba server.But i want my samba users to lo-gin from windows users and contain allocated amount of space. plz help me............ (1 Reply)
Discussion started by: yashwanthguru
1 Replies

3. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

4. Linux

Allocating available space to file system

have a VMWARE machine, I have extended it from 20GB to 30GB for Linux box. The linux box shows this for df -hal: Filesystem Size Used Avail Use% Mounted on -dev-mapper-VolGroup00-LogVol00 19G 5.9G 12G 34% - proc 0 0 0 - -proc sysfs 0 0 0 - -sys devpts 0 0 0 - -dev-pts -dev-sda1 99M 13M... (1 Reply)
Discussion started by: mackman
1 Replies

5. Shell Programming and Scripting

How to load different type of data in a file to two arrays

Hi, I have tried to find some sort of previous similar thread on this but not quite close to what I want to achieve. Basically I have two class of data in my file..e.g 1,1,1,1,1,2,yes 1,2,3,4,5,5,yes 2,3,4,5,5,5,no 1,2,3,4,4,2,no 1,1,3,4,5,2,no I wanted to read the "yes" entry to an... (5 Replies)
Discussion started by: ahjiefreak
5 Replies

6. Shell Programming and Scripting

SQL Data with Spaces into Arrays

Hi Simple thing has been driving me nuts. I have used the following code is ksh scripts to get data from Oracle table and then display it, allowing user to select one of the data options returned... REP_DATA=`sqlplus -s ${WMSDB} <<EOF SET SERVEROUTPUT ON FEEDBACK OFF... (0 Replies)
Discussion started by: mikem22
0 Replies

7. Programming

Process crash when allocating memory

Hi I'm currently using C++ on a HP-UX 11i system (upgrading some libraries) and am encountering a problem with the process crashing when allocating memory via a call to new (a rather large array of objects are being created). Is there a way to find out what the sizes of the stack and heap are?... (1 Reply)
Discussion started by: themezzaman
1 Replies

8. UNIX for Advanced & Expert Users

allocating swap space on solaris 9

Hi, I have a solaris 9-sparc box, which after bouncing is giving swap space related error messages(that swap space is not enough). could it be possible that there was some command issued or setting made before bouncing, which was lost after bouncing? please let me know how i can add swap space... (1 Reply)
Discussion started by: 0ktalmagik
1 Replies

9. UNIX for Dummies Questions & Answers

Re-allocating hard drive space

Hi, Is their an easy way to realloate hard drive space on Solaris 10. For example : /c20td0 10G /space 90 G I would like to move some of the hard-drive space from "/space" and add it to "/c20td0". In Windows this can be easily done using Partition magic, anything similar for UNIX? (4 Replies)
Discussion started by: annointed3
4 Replies

10. Shell Programming and Scripting

Reading in data sets into arrays from an input file.

Hye all, I would like some help with reading in a file in which the data is seperated by commas. for instance: input.dat: 1,2,34,/test for the above case, the fn. will store the values into an array -> data as follows: data = 1 data = 2 data = 34 data = /test I am trying to write... (5 Replies)
Discussion started by: sidamin810
5 Replies
Login or Register to Ask a Question