Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,547 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,573 people online right now. Registration is fast and FREE... Join Now!




returning ragged arrays?

 
Reply to this topicStart new topic

returning ragged arrays?

complexc25
25 Nov, 2007 - 03:36 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 13


My Contributions
lets say i create a ragged array inside a function by using pointers, how can i return the ragged array to the main function? (the function used arrays from the main fuction to calculate the ragged array)
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: Returning Ragged Arrays?
25 Nov, 2007 - 05:09 PM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
I am unfamiliar with the terminology.
Is a ragged array just an array, or some kind of object?

Anyway, you can let your function return a pointer to the thing.
Just make sure you handle its creation and destruction gracefully.
User is offlineProfile CardPM
+Quote Post

complexc25
RE: Returning Ragged Arrays?
25 Nov, 2007 - 05:16 PM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2007
Posts: 13


My Contributions
QUOTE(Trogdor @ 25 Nov, 2007 - 08:09 PM) *

I am unfamiliar with the terminology.
Is a ragged array just an array, or some kind of object?

Anyway, you can let your function return a pointer to the thing.
Just make sure you handle its creation and destruction gracefully.

Ragged arrays, also called jagged arrays are arrays of arrays,in which different rows can be different sizes.

I dont think i can return a value, because i created 2 of these ragged arrays and need both

This post has been edited by complexc25: 25 Nov, 2007 - 05:19 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Returning Ragged Arrays?
25 Nov, 2007 - 05:51 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
Then just pass appropriate pointer arguments to the function, and use those pointers in the creation of the arrays instead of a pointer created locally within the function.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Returning Ragged Arrays?
25 Nov, 2007 - 06:11 PM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
"ragged arrays" in C/C++ are accessed though double indirection:

char** raggedArray;

To pass such a beast by reference you just need "triple indirection" - a point to (a pointer to a pointer).

A ragged array is an array of pointers. Each of the elements points can point to a 1 dimentional array (of different lengths). So a pointer to the ragged array will work to pass this by referance:

CODE
int func(char ***PtrRaggedArray) {
    (*ptrRaggedArray)[1] = new char[10];
    ...etc...
}


you can simplify the syntax using a typedef:

typedef char ** RaggedArray;

then the function becomes:
CODE
int func(RaggedArray &PtrRaggedArray) {
    ptrRaggedArray[1] = new char[10];
    ...etc...
}


here is my little working example:
CODE
#include <cstring>
#include <iostream>

typedef char ** RaggedArray;
void func(char ***ptrToRaggedArray);
void foo(RaggedArray & array);

int main() {
    char **ptr;
    ptr = new char*[2];
    func(&ptr);
    foo(ptr);
    std::cout << "ptr[0]: " << ptr[0] << std::endl;
    std::cout << "ptr[1]: " << ptr[1] << std::endl;
    delete[] ptr[0];
    delete[] ptr[1];
    return 0;
}

void func(char ***ptrToRaggedArray) {
    (*ptrToRaggedArray)[0] = new char[100];
    strcpy((*ptrToRaggedArray)[0], "Hello World!");
    return;
}

void foo(RaggedArray & array) {
    array[1] = new char[100];
    strcpy(array[1], "Hello Nurse!");
    return;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month