Go Down

Topic: Storing an array pointer in an array! (Read 3151 times) previous topic - next topic

cowasaki

May 26, 2013, 02:03 pm Last Edit: May 26, 2013, 02:22 pm by cowasaki Reason: 1
I understand how to pass an array to a function, the following code shows how:

Code: [Select]
String test1[10];
String test2[10];

void setup() {
 // put your setup code here, to run once:
test1[0]="1";
test1[1]="2";
test1[2]="3";
test2[0]="one";
test2[1]="two";
test2[2]="three";
Serial.begin(115200);
 
}

void loop() {
 // put your main code here, to run repeatedly:
 
 testpassarray(test1);
 testpassarray(test2);
 
 
}

void testpassarray(String *elements){

Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
 
}



What I need to do is STORE the pointer in an array so:

(I know the program is pointless but I need to understand the concept!

Code: [Select]
String test1[10];
String test2[10];
unsigned long int stored[100];
int numarrays=0;

void setup() {
 // put your setup code here, to run once:
test1[0]="1";
test1[1]="2";
test1[2]="3";
test2[0]="one";
test2[1]="two";
test2[2]="three";
Serial.begin(115200);
 
}

void loop() {
 // put your main code here, to run repeatedly:
 
 testpassarray(test1);
 testpassarray(test2);
 
 
}

void testpassarray(String *elements){
 
stored[numarrays]=elements;
numarrays+=1;

Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
 
}


The code does not work.  I'm not at all new to programming just haven't got a big history of this language!

Also how can I tell how many elements inside the passed array?

Is the type "uint16_t" the correct one?  If so how do I store the address of the passed array so that I can use it in another function later?

Thanks all.



ADDED:

The following code shows what I am trying to do (JUST FOR THIS EXAMPLE) but clearly several lines are just rubbish

The lines which start "//////"  are the ones I probably need help with.

I know I could call the original function again BUT this is not what I am trying to do.  I need to understand HOW to do what this program is doing in order to add to an existing very large program.  So I have created this example just as a learning tool.

Code: [Select]



String test1[10];
String test2[10];
////// uint16_t stored[100];     // HOW ????
int numarrays=0;

void setup() {
 // put your setup code here, to run once:
test1[0]="1";
test1[1]="2";
test1[2]="3";
test2[0]="one";
test2[1]="two";
test2[2]="three";
Serial.begin(115200);

 testpassarray(test1);
 testpassarray(test2);

 reprint(0);
 reprint(1);
 
}

void loop() {
 // put your main code here, to run repeatedly:
 
 
 
}

void testpassarray(String *elements){
 
///// stored[numarrays]=elements;   // HOW ????
numarrays+=1;

Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
 
}

void reprint(unsigned long int whicharray){

///// setup variable "elements"         //  HOW ????
 
///// elements= address of which array  //  HOW ????


Serial.println(elements[0]);
Serial.println(elements[1]);
Serial.println(elements[2]);
 
}

PaulS

Quote
The code does not work.

Of course not. You are passing a pointer to an array of Strings to testpassarray. Then, in that function, you are creating, on the stack, an array of ints. Then, you are trying to set the first element beyond the end of the array to the value that is the address of the array of Strings.

Once the function ends, the array of ints goes away. The invalid assignment is lost.

You need to properly allocate space for the array, statically as a global array, or dynamically, using malloc() or new. The array MUST be of the right type to hold the pointers. You must assign the pointer to a valid element in the array.

There are just too many things you are doing wrong in that code.

cowasaki

#2
May 26, 2013, 02:33 pm Last Edit: May 26, 2013, 02:40 pm by cowasaki Reason: 1

Quote
The code does not work.

Of course not. You are passing a pointer to an array of Strings to testpassarray. Then, in that function, you are creating, on the stack, an array of ints. Then, you are trying to set the first element beyond the end of the array to the value that is the address of the array of Strings.

Once the function ends, the array of ints goes away. The invalid assignment is lost.

You need to properly allocate space for the array, statically as a global array, or dynamically, using malloc() or new. The array MUST be of the right type to hold the pointers. You must assign the pointer to a valid element in the array.

There are just too many things you are doing wrong in that code.


Thanks, I never expected the code to work but just that it could be followed.

The array "stored" needs to be an array of pointers to the arrays in the main program but I don't know what type the pointer is.  This array is global (as are the String arrays) so once the address of the array is stored it should stay where it is till I clear everything later.

In the other function "reprint" I need to get this pointer address back from the global array and then access the array based on that pointer.

The String arrays will not change between their pointers being placed into the global array and being used.  Certain events will completely clear all the arrays and we start again.

PaulS

Quote
but I don't know what type the pointer is.

Then, you are screwed. You can't store an array of pointers without knowing the type of the pointer.

Well, that is not technically accurate, since you could cast the pointer to type void *, but I really do NOT recommend that. There is a reason that C (and C++) is a strongly typed language.

Quote
This array is global

Not in the code you posted.

Quote
In the other function "reprint" I need to get this pointer address back from the global array and then access the array based on that pointer.

One assumption that you are making, that is incorrect, is that you can determine the size of the array that a pointer points to from just the pointer. You can't. So, somewhere, you also need to store the size of the array pointed to.

Quote
The String arrays will not change between their pointers being placed into the global array and being used.  Certain events will completely clear all the arrays and we start again.

You REALLY ought to consider not using Strings at all.

cowasaki

No I mean I don't know what type A pointer is. The arrays are all String arrays.

In the code I posted, the actual arrays are all global unless I am misunderstanding the scope, it's defined before the setup function in the main body not inside a function. Then the code sends the address of the array to the first function so I need to store that address whilst I am in the function. Then in the other function I need to recall the address from that array and then use the Strings. I can soon find the number of elements by looking for the first empty String.

So what type is a pointer? I assumed that it was a memory location hence my first guess at unsigned long int.
Once these addresses are stored in the global array how do I then use the address to refer to the String array?

Thanks

cowasaki

I have an array of 100 Strings for the main storage and these can be any length from empty to 200 characters but mainly empty or 1-10 characters so storing as a string array would take more space wouldn't it?

TanHadron

Storing as fixed size strings will probably not work well for you, then.  You might be able to pack them or make the space less if you know some information about how they are used.  Are you going to do a lot of text manipulation?  Is there a lot of inserting and deleting of strings?  Do they change a lot?

lloyddean

I keep revisiting your post but no matter how many times I read it I'm not understanding what it is you wish to accomplish.

cowasaki


I keep revisiting your post but no matter how many times I read it I'm not understanding what it is you wish to accomplish.



The progrmastere is just a small program that uses the processes I am trying to accomplish rather than  complicating matters by posting the entire program.

Basically I have about 20 different functions that all add gui objects to a master list then the interrupt driven routine creates the objects on screen. I need to be able to pass a list of Strings for certain objects but the master storage shouldn't duplicate the objects, I just want the add routine to store the array pointer. Once the array pointer is stored the draw routine for that type of object will recall the Strings and display them as necessary.

The boss of that that I can't work out how to do are store the array pointer in a cell of an array and then later recall that pointer using it to recall the Strings.

PaulS

Quote
No I mean I don't know what type A pointer is. The arrays are all String arrays.

An array of Strings is pointed to be a String pointer (String *).

lloyddean

Do you really need to use an array of 'String', or 'String' pointer, objects or can it  be an array of 'C' 'char*' (zero terminated string pointers)?

cowasaki


Quote
No I mean I don't know what type A pointer is. The arrays are all String arrays.

An array of Strings is pointed to be a String pointer (String *).


Thanks,  How do I assign that as a type for an array and once the values are in the array how do I then access element[1] etc from the array?

Sorry to be a pain but I know what I need to do but just don't have the experience with this language.

Cheers....

cowasaki


Do you really need to use an array of 'String', or 'String' pointer, objects or can it  be an array of 'C' 'char*' (zero terminated string pointers)?



Yes I do, I've mentioned it before I have an array of maybe 100 items which contain mainly "" or 5-10 chars BUT they could contain 200 characters.  If I used arrays of strings it would take 201x100 or nearly 20K but as Strings with most of them empty it uses probably 1-1.5K

PaulS

Quote
How do I assign that as a type for an array

Code: [Select]
String *lotsOfCarp[100];
will create an array of pointers to Strings.

cowasaki


Quote
How do I assign that as a type for an array

Code: [Select]
String *lotsOfCarp[100];
will create an array of pointers to Strings.


Many thanks.

Go Up