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

creating an array in a function

13
I'm trying to create a function which will return a pointer to an array, however this causes a massive number of errors, so I'm wondering if it is possible to do this in c++. This is the code I'm trying to use:

[PHP]int *getMatrix(int column, int row){
int sparseMatrix[column][row];
...
return &sparseMatrix[0];
}[/PHP]


I want to be able to use this function to create multiple arrays, however when I try to build the project, the declaration of sparseMatrix[][] gives me an error saying I'm allocating a constant size of 0 to it.

So, is it possible to create a function which can return a pointer to an array, where the array itself was created inside the function?
Or am I trying to make c++ do something that it isn't intended to do?

Thanks for any help!
Apr 1 '08 #1
4 1453
Laharl
849 Expert 512MB
You need to use new to create the pointer. As it stands, you have a local pointer to memory allocated on the stack. That memory goes out of scope when the function returns. Using new allocates the memory on the heap and returning the pointer ensures you have access to the memory outside the function. Just make sure you carefully manage your memory to avoid memory leaks.
Apr 1 '08 #2
whodgson
542 512MB
It may not be germaine but i`m not sure u can cross pollinate rows and columns like this.e.g. rows come first.....but since all multi dimensional arrays are really single dimensional it may not matter.
Apr 2 '08 #3
mmk
19
I'm trying to create a function which will return a pointer to an array, however this causes a massive number of errors, so I'm wondering if it is possible to do this in c++. This is the code I'm trying to use:

[PHP]int *getMatrix(int column, int row){
int sparseMatrix[column][row];
...
return &sparseMatrix[0];
}[/PHP]


I want to be able to use this function to create multiple arrays, however when I try to build the project, the declaration of sparseMatrix[][] gives me an error saying I'm allocating a constant size of 0 to it.

So, is it possible to create a function which can return a pointer to an array, where the array itself was created inside the function?
Or am I trying to make c++ do something that it isn't intended to do?

Thanks for any help!
Hi rolan,

I suggest you following code. Anyone please correct me if I am wrong.

Expand|Select|Wrap|Line Numbers
  1. int *getMatrix(int column, int row){
  2.     int sparseMatrix =new int [column][row]; 
  3.         ...
  4.        return &sparseMatrix[0];
  5. }
Thank you,
Mmk
Apr 2 '08 #4
rolan
13
Thanks so much, using the new operator worked wonderfully.
Apr 16 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
11
by: anto_kamau | last post by:
Question write a c program that displays a diamond pattern, when a user enters an odd number between 0 and 19, and displays on the screen
1
by: Rako | last post by:
My problem is: I want to create an index to any of the available picture-groups. This index is a table of thumbs with a scrollbar. If you click on the thumb, you get the full picture displayed. ...
3
by: creator_bob | last post by:
How do I create an array of datarows from a sorted list? I put a bunch of datarows into a sorted list to sort them. Then I got an array of the sorted elements. However, I cannot typecast them. ...
8
by: ctiggerf | last post by:
I was hopeing someone could help me out here. Been stumped on this one all day. This function 1. Checks uploaded files. 2. Creates two resized images from each (a full size, and a...
6
by: lawpoop | last post by:
Hello! I am working on a map of a rather large php project that I've been working on. I hope to create a map of the files of the project that would look like the output of the unix 'tree'...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
13
by: Justcallmedrago | last post by:
How would you declare and assign a variable inside a function THAT HAS THE NAME OF A PARAMETER YOU PASSED example: when you call createvariable("myvariable") it will declare the variable...
3
by: Bartholomew Simpson | last post by:
I am writing some C++ wrappers around some legacy C ones - more specifically, I am providing ctors, dtors and assignment operators for the C structs. I have a ton of existing C code that uses...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.