March 29, 2024, 05:26:57 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Another Pointer Question

Started by Rock Ridge Farm (Larry), August 08, 2006, 11:42:05 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Rock Ridge Farm (Larry)

string *describe[8] = {"First", "Second", "Third", "Forth", "Fifth", "Sixth", "Seventh", "eighth"};
The above will not compile - how should I do this?

John Syl.

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
Intel 3.6 p4 ht, XP home,2 gb mem, 400 gb hd 20gb raid 0, Nvidia 6600le.
AMD k6-2 500, 40gb.

Started on PDP11 Assembler, BASIC, GWBASIC, 6502, Z80, 80x86, Java, Pascal, C, C++, 
IBasic (std & pro), Aurora, EBasic.  (Master of none, but it's been fun!)

kryton9

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;
}



Ionic Wind Support Team

Strings are an array, and an array of pointers to strings would be a two dimensional array.  I'll let you think about that.
Ionic Wind Support Team

John Syl.

August 09, 2006, 12:12:50 PM #4 Last Edit: August 09, 2006, 12:18:36 PM by puddytat
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!!

Intel 3.6 p4 ht, XP home,2 gb mem, 400 gb hd 20gb raid 0, Nvidia 6600le.
AMD k6-2 500, 40gb.

Started on PDP11 Assembler, BASIC, GWBASIC, 6502, Z80, 80x86, Java, Pascal, C, C++, 
IBasic (std & pro), Aurora, EBasic.  (Master of none, but it's been fun!)

Ionic Wind Support Team

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.
Ionic Wind Support Team

John Syl.

 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

Intel 3.6 p4 ht, XP home,2 gb mem, 400 gb hd 20gb raid 0, Nvidia 6600le.
AMD k6-2 500, 40gb.

Started on PDP11 Assembler, BASIC, GWBASIC, 6502, Z80, 80x86, Java, Pascal, C, C++, 
IBasic (std & pro), Aurora, EBasic.  (Master of none, but it's been fun!)

Rock Ridge Farm (Larry)

Will the same hold true for an array of integers?
int x[10];
x = 1,2,3,4,5,6,7,8,9,0;

Rock Ridge Farm (Larry)

Also - can I define a structure then have struct mystruct *poopoo; or do I also delete the * here?