IonicWind Software

Aurora Compiler => General Discussion => Topic started by: Rock Ridge Farm (Larry) on August 08, 2006, 11:42:05 AM

Title: Another Pointer Question
Post by: Rock Ridge Farm (Larry) on August 08, 2006, 11:42:05 AM
string *describe[8] = {"First", "Second", "Third", "Forth", "Fifth", "Sixth", "Seventh", "eighth"};
The above will not compile - how should I do this?
Title: Re: Another Pointer Question
Post by: John Syl. on August 08, 2006, 12:54:46 PM
Strings are already referred to by a pointer, if you try and access with another pointer you get the address of the string, not the string.

Try this:

string describe[8];

global sub main()
{
openconsole();
describe = "First", "Second", "Third", "Forth", "Fifth", "Sixth", "Seventh", "eighth";
print (describe[0]);
print (describe[2]);
print (describe[4]);
print (describe[7]);
text();
do{}until getkey() <> "";
return;
}

sub text()
{
print (describe[1]);
return;
}


As the string array is defined outside a routine they are global proved by the call to sub text().
The variable cannot be defined outside of a routine.

John
Title: Re: Another Pointer Question
Post by: kryton9 on August 08, 2006, 05:36:07 PM
I know arrays are pointers, but I was trying to make the same code using a pointer and got nowhere.
string *describe[8];

global sub main()
{
openconsole();
describe[0] = "First";
describe[1] = "Second";// "Third", "Forth", "Fifth", "Sixth", "Seventh", "eighth";
print (*describe[0]); // this prints out correctly
print (*describe[1]); // this does not print
text();
do{}until getkey() <> "";
return;
}

sub text()
{
print (*describe[0]); // this works here
return;
}


Title: Re: Another Pointer Question
Post by: Ionic Wind Support Team on August 08, 2006, 05:54:43 PM
Strings are an array, and an array of pointers to strings would be a two dimensional array.  I'll let you think about that.
Title: Re: Another Pointer Question
Post by: John Syl. on August 09, 2006, 12:12:50 PM
I've had a think and found that if you assign the strings to string variables then assign the pointers to the pointer array using the pointers of the string variables, all is well, i.e.

STRING *describe[8];
STRING a[8];

global sub main()
{
a[0]="First";
a[1]="Second";
a[2]="Third";
a[3]="Fourth";
a[4]="Fifth";
a[5]="Sixth";
a[6]="Seventh";
a[7]="Eighth";
openconsole();
describe[0] = a[0];
describe[1] = a[1];
describe[2] = a[2];
describe[4] = a[4];
describe[7] = a[7];
print (*describe[0]);
print (*describe[1]);
print (*describe[2]);
print (*describe[4]);
print (*describe[7]);
text();
do{}until getkey() <> "";
return;
}

sub text()
{
print (*describe[3]);
return;
}


So it obviously has something to do with the pointers to the the literal assignment.... ???

As for the array array of pointers to strings being two dimensional...my mind keeps coming to a single array conclusion but it doesn't work, nor can I fathom the 2 dimensional solution....and I thought I understood pointers!!

Title: Re: Another Pointer Question
Post by: Ionic Wind Support Team on August 09, 2006, 01:29:48 PM
OK here is the solution.

Your array is an array of pointers.  Each pointer contains the address of another array, in this case an array of characters.  Because of precedence rules this:

*describe[1];

If derferencing first and then taking the array operator.  You want it the other way around, the array operator into the array of pointers and then a dereference.  So the answer is:

*(string)(describe[1]);

The typecast is necessay because using (describe[1]) loses the original type and return the value indexed into the pointer array.

Paul.
Title: Re: Another Pointer Question
Post by: John Syl. on August 09, 2006, 01:45:05 PM
 I think i tried every which way including the *(string) typecast....but never even thought of the bracketed describe[] reference!!  Not that I could have explained why if I had cracked it.  Thanks for the explanation Paul.

John

Title: Re: Another Pointer Question
Post by: Rock Ridge Farm (Larry) on August 12, 2006, 03:39:35 PM
Will the same hold true for an array of integers?
int x[10];
x = 1,2,3,4,5,6,7,8,9,0;
Title: Re: Another Pointer Question
Post by: Rock Ridge Farm (Larry) on August 12, 2006, 04:39:56 PM
Also - can I define a structure then have struct mystruct *poopoo; or do I also delete the * here?