error 169: Invalid assignment/conversion [FBC 0.90.1]

General FreeBASIC programming questions.
Post Reply
creek23
Posts: 261
Joined: Sep 09, 2007 1:57
Location: Philippines
Contact:

error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by creek23 »

For some reason, I have this stub of code that I (or somebody else) might have added a long time ago for some basic encryption-ish, idk.

Code: Select all

PUBLIC FUNCTION trimOdds(oldStr As STRING) AS STRING
	DIM AS STRING newStr = STRING(LEN(oldStr) / 2," ")
	DIM ii AS INTEGER
	
	FOR i AS INTEGER = 0 TO LEN(oldStr) STEP 2
		newStr[ii] = CHR(oldStr[i])
		ii = ii + 1
	NEXT
	RETURN newStr
END FUNCTION
What it does is it takes the chars from even-numbered index. ex: "HZEDLTLHOM" would supposedly return "HELLO", if I remember correctly.

It was working fine with FBC 0.24, but not in 0.90.1 as I'm getting an:

Code: Select all

error 169: Invalid assignment/conversion in `newStr[ii] = CHR(oldStr[i]))'
I know I could write some other routine arriving in the same result back when it was working but I'd like to know the rationale why it's not working now to avoid same errors in the future.

~creek23
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by dkl »

Hmm, maybe that line:

Code: Select all

		newStr[ii] = CHR(oldStr[i])
should just be this:

Code: Select all

		newStr[ii] = oldStr[i]
in order to copy over a character, that should be enough.

There have been some changes in this area in 0.90.0, that made the compiler more strict overall. Especially this bug comes to mind: #577. The [] string indexing is a way to access the individual chars of the string, but chr() returns a string, and strictly speaking it's not possible to assign a string to a character; that was the main reason behind disallowing it.
creek23
Posts: 261
Joined: Sep 09, 2007 1:57
Location: Philippines
Contact:

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by creek23 »

dkl wrote:chr() returns a string
o.O

Makes sense.

Thanks again, dkl!

~creek23
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by fxm »

Remark:
I would have hoped that the compiler goes further in its rigor on the assignment with respect to the types.
For example, I think this following assignment should be forbidden:

Code: Select all

Dim As String s = "Test string"
Dim As Zstring * 4 z = "###"
Print s
s[4] = *@z ' should flag a compile error
Print s
Sleep
#Print Typeof(s[4])
#Print Typeof(*@z)

Code: Select all

Test string
Test#string

Code: Select all

UBYTE
ZSTRING * 1
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by dkl »

I think that's ok, it's just copying one char, not the whole string. *@z is dereferencing a Zstring Ptr, which can be treated as string indexing in the context of integer expressions (some more info), such as assignments to a ubyte (s[4]).
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by fxm »

dkl wrote:I think that's ok, it's just copying one char, not the whole string. *@z is dereferencing a Zstring Ptr, which can be treated as string indexing in the context of integer expressions (some more info), such as assignments to a ubyte (s[4]).
Perhaps yes!!!

But between:

-1):

Code: Select all

Dim As String s = "Test string"
Dim As Zstring Ptr pz = @"#%&"
Print s
Print *pz
s[4] = *pz ' should flag a compile error
Print s
Sleep
#Print Typeof(s[4])
#Print Typeof(*pz)

Code: Select all

Test string
#%&
Test#string

Code: Select all

UBYTE
ZSTRING * 1
and

-2):

Code: Select all

Dim As String s = "Test string"
Dim As Zstring Ptr pz = @"#%&"
Print s
Print (*pz)[0]
s[4] = (*pz)[0] ' better syntax
Print s
Sleep
#Print Typeof(s[4])
#Print Typeof((*pz)[0])

Code: Select all

Test string
35
Test#string

Code: Select all

UBYTE
UBYTE
I will always prefer the second syntax 's[4] = (*pz)[0]' because this assignment syntax is more rigorous (Ubyte = Ubyte).
Dereferencing a zstring ptr should always return a zstring, not a ubyte depending on context!
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by dkl »

It certainly is...

I'm preferring it too actually, but only since I've learned about it. Previously I expected zstringptr[index] to work as in C, i.e. to return the char. Of course it doesn't always work like that, but maybe this similarity to C was one of the reasons it was made to work like that, even though it couldn't be fully like C, due to FB's built-in string support and other "quirks".

(*zstringptr)[index] is pretty weird, it looks like a double dereference, even though it's doing just one, but that's because FB uses [] for string indexing. For dynamic strings that makes sense again because there it actually is dereferencing the descriptor's data pointer...
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by fxm »

dkl wrote:(*zstringptr)[index] is pretty weird, it looks like a double dereference, even though it's doing just one, but that's because FB uses [] for string indexing.
Instead of operator '[]', we could have use the operator '()' like for array index!
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by Roland Chastain »

Hello!

I try to compile with FBC 0.90.1 a Lua interpreter posted sometime ago in the forum. I get an error 169. Maybe the solution has been given in the discussion above, but I wasn't able to find it.

Code: Select all

' http://www.freebasic.net/forum/viewtopic.php?f=14&t=10836#p94556

' Example 1: A line-by-line stand-alone Lua interpreter 
' (Takes a single parameter, the name of the lua script to execute)
 
'Include the Lua libraries
#Include Once "Lua/lua.bi"
#Include Once "Lua/lauxlib.bi"
#Include Once "Lua/lualib.bi"

'Since we're imitating C, everything goes in a main function which
'returns an integer
Function main (arg As String) As Integer
  Dim As String finp
  'We have to use ZStrings, since Lua is written in C
  Dim buff As ZString*256
  
  Dim chunk_name As ZString*4
  chunk_name = "line"
  
  Dim As ZString Ptr err_str
  Dim error_value As Integer
  
  Dim frf As uInteger

  'Create the Lua state
  'Dim As lua_State Ptr L = lua_open()   ' opens Lua ' error 41: Variable not declared, lua_open
  Dim As lua_State Ptr L = luaL_newstate()
  
  'Start the standard libraries  
  luaopen_base(L)                     ' opens the basic library
  luaopen_table(L)                    ' opens the table library
  luaopen_io(L)                       ' opens the I/O library
  luaopen_string(L)                   ' opens the string lib.
  luaopen_math(L)                     ' opens the math lib.

  'Open the file we're going to interpret
  frf = FreeFile
  Open arg For Input As #frf

  'Read until we reach the end of it
  Do While Not Eof(frf)
    'Get each line into the temporary String buffer
    Line Input #frf, finp

    'Now convert it to ZString
    buff = finp

    'If it's not a blank line, pass it to Lua and execute it
    If finp <> "" Then
      error_value = luaL_loadbuffer(L, buff, Len(buff), chunk_name)
      lua_pcall(L, 0, 0, 0)

      'If an error occurs, print the error message
      If error_value <> 0 Then
        err_str = lua_tostring(L, -1) ' error 169: Invalid assignment/conversion
        Print "Error:  " + *err_str
  
        lua_pop(L, 1)  'Pop error message from the stack
      End If
    End If
  Loop

  'Close the file
  Close #frf

  'Shutdown the Lua state
  lua_close(L)

  'Return the error value, if any error occurs
  Return error_value
End Function

'Store the value returned by the main function
Dim As Integer retval

retval = main(Command)

'End with that error code (0 if no error occurs)
End retval
 
Here are what I found in lua.bi:

C:\FreeBASIC\inc\Lua\lua.bi:88:declare function lua_tolstring(byval L as lua_State ptr, byval idx as long, byval len as uinteger ptr) as const zstring ptr
C:\FreeBASIC\inc\Lua\lua.bi:191:#define lua_tostring(L,i) lua_tolstring(L, (i), NULL)


I don't see what is wrong in that assignment, and I can't find a workaround.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by dodicat »

If I use
Dim As const ZString Ptr err_str
then I get no errors, the return code is 0.
(Win XP)
I use the GIT distribution as given by MOD a while back
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by fxm »

- First bug:
Dim chunk_name As ZString*5
chunk_name = "line"


- Second bug:
Dim As Const ZString Ptr err_str

[edit]
Dodicat, you just beat me for the second (main) bug!
Roland Chastain
Posts: 1003
Joined: Nov 24, 2011 19:49
Location: France
Contact:

Re: error 169: Invalid assignment/conversion [FBC 0.90.1]

Post by Roland Chastain »

Thank you dodicat and fxm!
Post Reply