dimensioning arrays

New to FreeBASIC? Post your questions here.
GreyWolf
Posts: 8
Joined: Dec 06, 2013 20:15
Location: Kansas City, Missouri USA

dimensioning arrays

Postby GreyWolf » Jun 17, 2015 21:43

Hi again --

I am trying to use a single-dimension array- place( 1 to 4 ) - to gather data in one procedure and pass by-reference ( in the paramenter list ) back to the calling procedure. During compilation, I continually get compilation errors each time I attempt to cite the array to gather any data in the called procedure, i.e. "Array not dimensioned, before '(' in 'place( 1 ) = AGX1 + ToknBrdr'".

I have tried "Dim Shared As Integer place ( 1 to 4 )" just after the preprosser directives, I have tried "Dim As Integer
place( 1 to 4 )" and "Dim As Integer place( 4)" in both the calling routine and the called routine(at different times). Still the same compiler error msgs. Oh, BTW, the error messages are always directed at the array usage in the called routine; that possibly might be due to the fact that the called routine physically is between the start of the program and the calling routine.

I do not need nor necessarily want to use a dynamic array, so I believe it should not be this obtuse. In any event, I have a long way to go to become proficient!

Help Please!!! What am I not understanding about declaring and using arrays???
BasicCoder2
Posts: 3433
Joined: Jan 01, 2009 7:03

Re: dimensioning arrays

Postby BasicCoder2 » Jun 17, 2015 21:50

@"GreyWolf",
Rather than "explain" your problem you might find a faster reply if you actually include the code causing the problem.
fxm
Posts: 9474
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: dimensioning arrays

Postby fxm » Jun 18, 2015 8:46

BasicCoder2 wrote:@"GreyWolf",
Rather than "explain" your problem you might find a faster reply if you actually include the code causing the problem.
and with the FreeBASIC dialect (lang) that you are using.
SARG
Posts: 950
Joined: May 27, 2005 7:15
Location: FRANCE

Re: dimensioning arrays

Postby SARG » Jun 18, 2015 10:17

Hi,

is it the only error ?
With the code below as 'place' is also defined in the called routine I got the same error message.

Code: Select all

Sub test
   Dim As Integer place
   place(1)=999
End Sub
Dim  Shared  As Integer place(1)
Tourist Trap
Posts: 2792
Joined: Jun 02, 2015 16:24

Re: dimensioning arrays

Postby Tourist Trap » Jun 18, 2015 14:05

Code: Select all

    Sub test
       ''Local scope
        Dim As Integer place()      ''This local var as no binding with place() defined at global level, even if name is the same because local variable are hidden unless special keywords (like shared in global....)
        place(1)=999
    End Sub
   
    ''------------------------------------------------------------------
    ''Global scope
    Dim  As Integer place(1)    ''This is defined but not passed to the Sub
                                        ''you didn't even call the sub
                                        ''I removed Shared, it was not fitting my explaination ;--)


Try this :

Code: Select all

Sub test( copyOfPlace() As Integer)
   'local scope
   ReDim Preserve copyOfPlace(UBound(copyOfPlace) + 1)
   copyOfPlace   (UBound(copyOfPlace)) = 999
End Sub


'Global scope
Dim  As Integer place()
   
test( place() )   ''call test and pass place() array to it

? place(0)       '' Now place(0) is created

test( place() )  ''call test and pass place() array to it

? place(1)       '' Now place(1) is created, place(0) still exists due to preserve keyword

sleep 

Sorry if it's not fully explained, or even the right answer to your question, I dont master every points. However, this should work as you expected.
In fact the only problem I see now about your first code, is that you didn't call your sub. If you dont do that it wont be very interesting, but I dont see where is the error , unless for a compiler option as said before. Oh yes, you redefined a local variable hidding the global one. Sorry I'm busy so I'm melting everything ;)
SARG
Posts: 950
Joined: May 27, 2005 7:15
Location: FRANCE

Re: dimensioning arrays

Postby SARG » Jun 18, 2015 21:26

@Tourist Trap

LOL
That's just an example of code genarating a such error and so, maybe, the reason of the error. No need to explain it.....

GreyWolf has to show his own code.
GreyWolf
Posts: 8
Joined: Dec 06, 2013 20:15
Location: Kansas City, Missouri USA

Re: dimensioning arrays

Postby GreyWolf » Jun 19, 2015 3:05

Hey folks,

Thank you for the quick responses!!! They were helpful!

Based on the responses from you all, I went back and recreated the problematic code, see below:

' global declartion
Dim As Integer shared place( 1 to 4 )
Declare Sub O_Token( As UInt, As UInt )
Declare Sub Get_Location( As UInt, ByRef As Integer )

Sub O_Token( use As UInt, box As UInt ) ' calling routine
Dim As UInt i
Dim As Integer place( 1 to 4 )
...............
Elseif use = 2 Then
Get_Location ( box, place( 4 ) )
.......
End If
End Sub

Sub Get_Location ( box As UInt, ByRef place As Integer ) ' called routine
Dim i As integer
' -------------------------------------
Select Case As Const box
Case 1
place( 1 ) = AGX1 + ToknBrdr
place( 2 ) = place( 1 ) + ToknEdge
place( 3 ) = AGY1 + ToknBrdr
place( 4 ) = place( 3 ) + ToknEdge
..........................
Case Else
End Select
End Sub

The above code resulted with the complilation errors shown in first post. Then I modified the above code as follows :

' global declartion
Dim As Integer shared place( 1 to 4 )
Declare Sub O_Token( As UInt, As UInt )
Declare Sub Get_Location( As UInt ) <<<===

Sub O_Token( use As UInt, box As UInt ) ' calling routine
Dim As UInt i
<<<===
...............
Elseif use = 2 Then
Get_Location ( box ) <<<===
.......
End If
End Sub

Sub Get_Location ( box As UInt) ' called routine <<<===
Dim i As integer
' -------------------------------------
Select Case As Const box
Case 1
place( 1 ) = AGX1 + ToknBrdr
place( 2 ) = place( 1 ) + ToknEdge
place( 3 ) = AGY1 + ToknBrdr
place( 4 ) = place( 3 ) + ToknEdge
..........................
Case Else
End Select
End Sub

The modified statements are identified with the <<<=== symbol. The compilation went merrily to a successful completion. This approach seems logical and useful but I still do not understand why I can't simply locally declare the array in the calling routine and pass it in the parameter list to the called routine. Will work on that at a later time, but if you see where I went wrong initially, please let me know.

Again, Thanks to all for responding with clues as to where a solution to the problem might lie!
Merick
Posts: 1038
Joined: May 28, 2007 1:52

Re: dimensioning arrays

Postby Merick » Jun 19, 2015 5:03

If you want to use an array as a parameter, you need to tell that to the to the procedure:

Declare Sub Get_Location( (1 to 4) As UInt )

Sub Get_Location ( box (1 to 4) As UInt)
fxm
Posts: 9474
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: dimensioning arrays

Postby fxm » Jun 19, 2015 6:26

In what language are you coding?
Not in FreeBASIC I suppose!
Drago
Posts: 116
Joined: Aug 10, 2005 13:15

Re: dimensioning arrays

Postby Drago » Jun 19, 2015 7:04

There are two ways to code:

use dim shared:

Code: Select all

Dim shared as integer place( 1 to 4 )

Declare Sub O_Token( box As UInteger, use As UInteger )
Declare Sub Get_Location( box As UInteger )

Sub Get_Token( box As UInteger, use As UInteger ) ' calling routine
   '...............
   if use = 1 then
      'nothing to do here
   Elseif use = 2 Then
      Get_Location ( box )
      '.......
   End If
End Sub

Sub Get_Location ( box As UInteger ) ' called routine
   ' -------------------------------------
   Select Case As Const box
   Case 1
      place( 1 ) = 10 + 5
      place( 2 ) = place( 1 ) + 5
      place( 3 ) = 100 + 5
      place( 4 ) = place( 3 ) + 5
      '..........................
   Case Else
      'nothing to do here
   End Select
End Sub

'Main

Get_token(1,2)

print place(1)
print place(2)
print place(3)
print place(4)

sleep


or hand over the var down to the functions:

Code: Select all

Dim as integer place( 1 to 4 )

Declare Sub O_Token( box As UInteger, use As UInteger, place() as integer )
Declare Sub Get_Location( box As UInteger, place() as integer )

Sub Get_Token( box As UInteger, use As UInteger, place () as integer ) ' calling routine
   '...............
   if use = 1 then
      'nothing to do here
   Elseif use = 2 Then
      Get_Location ( box, place() )
      '.......
   End If
End Sub

Sub Get_Location ( box As UInteger, place() as integer ) ' called routine
   ' -------------------------------------
   Select Case As Const box
   Case 1
      place( 1 ) = 10 + 5
      place( 2 ) = place( 1 ) + 5
      place( 3 ) = 100 + 5
      place( 4 ) = place( 3 ) + 5
      '..........................
   Case Else
      'nothing to do here
   End Select
End Sub

'Main

Get_token(1,2,Place())

print place(1)
print place(2)
print place(3)
print place(4)

sleep


In case of dim shared there is no need to dim vars in the subs.

Greetings
Drago
GreyWolf
Posts: 8
Joined: Dec 06, 2013 20:15
Location: Kansas City, Missouri USA

Re: dimensioning arrays

Postby GreyWolf » Jun 19, 2015 15:49

Good Morning All,

I have been reviewing the responses, taking some of the code that has been suggested and compiling on my machine. It all compiled well, so I had to go back and dig some more to find the difference between your codes and mine.

OH BTW, I am using FBIde 0.4.6 with fbc.exe, ver.1.02.0 on Win 8.1 64-bit OS. Is this the latest release?

In any event, I found where I have been omitting one issue from my discussion ( my assumption -- you know what assume does -- that this issue made no difference was idiotic ). Please accept my heart-felt apologizes and know that I have learned the lesson.
The problem was that I was trying to pass the array place( 1 to 4 ) ByRef. i.e. :
Declare Sub Get_Location( As UInt, ByRef place() As Integer )

Sub Get_Location ( box As UInt, ByRef place() As Integer )

This, it appears, is what was creating my compiler errors about which I was asking for help in the first place. I eliminated the references to passing ByRef and everything compiles nicely.
Declare Sub Get_Location( As UInt, place() As Integer )

Sub Get_Location ( box As UInt, place() As Integer )

I have yet to test the program to determine if it functions as designed. The intent is that the data generated in the called routine be available for use in the calling routine. Hopefully, this is a quirk with arrays and nothing more.

My sincere thanks to each of you for your responses and help. You directed me to the area in which I was having trouble and for that I am deeply grateful.
MrSwiss
Posts: 3348
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: dimensioning arrays

Postby MrSwiss » Jun 19, 2015 15:56

@GreyWolf,

the current 'production' compiler version is: FBC 1.02.1 at:
http://freebasic.net/get
GreyWolf
Posts: 8
Joined: Dec 06, 2013 20:15
Location: Kansas City, Missouri USA

Re: dimensioning arrays

Postby GreyWolf » Jun 20, 2015 3:49

Thanks MrSwiss

Return to “Beginners”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 2 guests