Can default for a Structure be a null?

Just starting out? Need help? Post your questions and find answers here.
WoodLark
User
User
Posts: 15
Joined: Thu Apr 14, 2005 12:57 pm
Location: South Carolina, USA

Can default for a Structure be a null?

Post by WoodLark »

Is this a valid procedure declaration in Purebasic 4.50?

Procedure FindRecords(db, recstructure = #NULL)
...
EndProcedure

Where db is a database handle and recstructure is a structure.

My question is actually "Can the default value for a structure be a NULL?"
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Can default for a Structure be a null?

Post by Trond »

#NULL is just a constant with the value 0. The procedure is perfectly valid, if you pass only one parameter when calling the procedure, recstructure will be 0 inside the procedure.

The only problem with your question is that you mention "structure", but recstructure is not a structure, it's a normal integer.
WoodLark
User
User
Posts: 15
Joined: Thu Apr 14, 2005 12:57 pm
Location: South Carolina, USA

Re: Can default for a Structure be a null?

Post by WoodLark »

Thank you Trond,

I fear I'm not using the proper method. I want to be able to pass a structure to the procedure, but need to handle the case where no values have been defined within the structure.
Would this work?

Procedure FindRec(db,RecStructure)
If RecStructure
....
Else
....
Endif

or this?

Procedure FindRec(db, RecStructure)
If RecStructure = ClearStructure(RecStructure,StructureDefinition)
....
Else
....
Endif

I worry that the last method might actually clear the structure, but maybe the use of a "dummy" structured variable would work

Procedure FindRec(db, RecStructure)
If RecStructure = ClearStructure(DummyStructure,StructureDefinition)
....
Else
....
Endif
Any thoughts?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Can default for a Structure be a null?

Post by Trond »

I want to be able to pass a structure to the procedure, but need to handle the case where no values have been defined within the structure.
Values are always defined. They are initialized to 0 automatically when a variable is declared or when you allocate memory.

None of your procedures accept a structure as the parameter, so I still don't understand what your problem is.
User avatar
Demivec
Addict
Addict
Posts: 4257
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Can default for a Structure be a null?

Post by Demivec »

WoodLark wrote:I fear I'm not using the proper method. I want to be able to pass a structure to the procedure, but need to handle the case where no values have been defined within the structure.
Using only the limited information you've provided:

Code: Select all

Structure someStructure
  ;....
EndStructure


Procedure FindRec(dB,*RecStructure.someStructure = #Null)
  ;test the value of the pointer
  If *RecStructure 
    ;an address to a structure was supplied
    ;....
  Else
    ;a #Null value for structure
    ;....
  EndIf
  ;....
EndProcedure


Define dB, Rec.someStructure

;pass a structure
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec) 

;don't pass a structure
FindRec(dB) ;uses default of #Null for structure
GBeebe
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Oct 09, 2004 6:52 pm
Location: Franklin, PA - USA
Contact:

Re: Can default for a Structure be a null?

Post by GBeebe »

The code that Demivec posted will work if you don't pass a structure to the procedure. But I think what you need to do is check each item in the structure, in the procedure, to see if they are 0 or "" or whatever.

You can create an empty version and compare the data pretty quckly. A modified version of Demivec's code:

Code: Select all

Structure someStructure
a.f
b.i  
c.s  
EndStructure


Procedure FindRec(dB,*RecStructure.someStructure = #Null)
  fake.someStructure
  
  ;test the value of the pointer
  If *RecStructure
    ;an address to a structure was supplied
    ;see if it is empty:
      If PeekS(@fake, SizeOf(someStructure)) <> PeekS(*RecStructure, SizeOf(someStructure))
        ;it is not empty, you may continue.
        Debug "Not empty"
        ;....
     Else
        Debug "Empty"
        ;It is empty
     EndIf
  Else
    ;a #Null value for structure
    Debug "Is NULL"
    ;....
  EndIf
  ;....
EndProcedure


Define dB, Rec.someStructure

;pass a structure
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec)

;don't pass a structure
FindRec(dB) ;uses default of #Null for structure

;now we'll fill in some of the structure
Rec\a = 1.05
Rec\b = 20
Rec\c = "Hi"
FindRec(dB, Rec)
Also, To answer your original question:
My question is actually "Can the default value for a structure be a NULL?"

The "default value" of a Structure is the address of where that Structure resides in memory, so even if it is Empty, it will have a value Unless it doesn't even exist. If you declare a Structure, it will exist even if it is Empty.

Hope this helps.
Last edited by GBeebe on Mon Jun 14, 2010 2:17 am, edited 2 times in total.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Can default for a Structure be a null?

Post by Kaeru Gaman »

I think there is an understanding problem:
Structures are only passed by Reference, never by Value.
so, when you want to pass a stuctured Value, you need to pass a pointer to a Stucture outside the Procedure.
the stuctured Variable can be empty, e.g. every field = 0,
but the Value passed to the Procedure will be a valid pointer to the empty Structure.
(or like Demivec showed, replaced by a #NULL pointer to indicate the Rect outside wasn't assigned)

...

when you want to pass a Structure byVal to a Procedure, you'll need to build a container first, and it will be limited to 64bit of size.
you can pass a Rectangle byVal, when you pass it as a Quad containing four Word-Size Coordinates.
oh... and have a nice day.
WoodLark
User
User
Posts: 15
Joined: Thu Apr 14, 2005 12:57 pm
Location: South Carolina, USA

Re: Can default for a Structure be a null?

Post by WoodLark »

I am not making myself clear; I apologize. Here (I hope) is a better example of what I am trying to accomplish:

Structure ARecord
field1.q
field2.q
field3.s
EndStructure

Procedure FindRec(db, MyRecord.ARecord )
If MyRecord
;Do something
Else ; none of the variables in the structure have been assigned any values other than their defaults (as if a ClearStructure() had been performed just before the call).
;Do something different
Endif
EndProcedure

Does this make any more sense?
GBeebe
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Oct 09, 2004 6:52 pm
Location: Franklin, PA - USA
Contact:

Re: Can default for a Structure be a null?

Post by GBeebe »

Reading it again I stand firm that my my previous reply is what you're looking for.

Here, I modified it a bit to match your ARecord structure:

Code: Select all

Structure ARecord
field1.q
field2.q
field3.s
EndStructure


Procedure FindRec(dB, *MyRecord.ARecord = #Null)
  fake.ARecord
  
  ;test the value of the pointer
  If *MyRecord
    ;an address to a structure was supplied
    ;see if it is empty:
      If PeekS(@fake, SizeOf(ARecord)) <> PeekS(*MyRecord, SizeOf(ARecord))
        ;it is not empty, you may continue.
        Debug "Here MyRecord IS NOT Empty"
        ;Do something
     Else
        Debug "Here MyRecord IS Empty"
        ;Do something different
     EndIf
  Else
    ;for some reason, you may never need it, but here MyRecord doesn't exist.
    Debug "Here MyRecord IS NULL"
    ;....
  EndIf
  ;....
EndProcedure


Define dB, Rec.ARecord

;pass a structure
;This is an Empty Record.
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec)

;don't pass a structure
;No record is passed, which will may *MyRecord = #Null or zero.
FindRec(dB) ;uses default of #Null for structure

;now we'll fill in some of the structure so it IS NOT EMPTY.
Rec\field1 = 10
Rec\field2 = 555
Rec\field3 = "Something"
FindRec(dB, Rec)
WoodLark
User
User
Posts: 15
Joined: Thu Apr 14, 2005 12:57 pm
Location: South Carolina, USA

Re: Can default for a Structure be a null?

Post by WoodLark »

Thank you GBeebe,

I think I understand now. I'll give it a try.
GBeebe
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Oct 09, 2004 6:52 pm
Location: Franklin, PA - USA
Contact:

Re: Can default for a Structure be a null?

Post by GBeebe »

Not a problem, WoodLark. I'm still amazed that you've been a member since '05 and up until today, you've only had 6 posts.
User avatar
Demivec
Addict
Addict
Posts: 4257
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Can default for a Structure be a null?

Post by Demivec »

@WoodLark: Here is a code sample with a slight variation in the way that an empty structure is tested for. It varies from GBeebe's in only one or two minor ways.

Code: Select all

Structure ARecord
  field1.q
  field2.q
  field3.s
EndStructure

Procedure FindRec(dB, *MyRecord.ARecord) ;structures are passed by reference, i.e. a pointer
  Static blankRecord.ARecord ;empty record
  
  If *MyRecord
    If CompareMemory(*MyRecord, @blankRecord, SizeOf(ARecord)) = 0
    ;Do something
      Debug "Non-empty structure supplied."
    Else ;all of the structure's values are the same as as a new structure
    ;Do something different
      Debug "Empty record supplied."
    EndIf
  Else ;a value of #Null was supplied for the address of ARecord
    ;Do nothing or handle error
    Debug "Error, record pointer is #Null."
  EndIf
EndProcedure


Define dB, Rec.ARecord

;This is an Empty Record.
FindRec(dB, Rec) ;or the equivalent FindRec(dB, @Rec)

;No record is passed, i.e. #Null)
FindRec(dB, #Null) 

;now we'll fill in some of the structure so it IS NOT EMPTY.
Rec\field1 = 10
Rec\field2 = 555
Rec\field3 = "Something"
FindRec(dB, Rec)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Can default for a Structure be a null?

Post by Kaeru Gaman »

WoodLark wrote:

Code: Select all

Procedure FindRec(db, MyRecord.ARecord ) 
Structures cannot be passed by Value, only by Reference, means you need a pointer there.
oh... and have a nice day.
WoodLark
User
User
Posts: 15
Joined: Thu Apr 14, 2005 12:57 pm
Location: South Carolina, USA

Re: Can default for a Structure be a null?

Post by WoodLark »

GBeebe wrote:Not a problem, WoodLark. I'm still amazed that you've been a member since '05 and up until today, you've only had 6 posts.
When I joined in '05, I was gainfully employed and was required to use vb.net. Since I retired, I've only written one small Purebasic app, but now I decided to take on a more ambitious project. I develop in Linux, but want to be able to compile cross-platform apps.
GBeebe
Enthusiast
Enthusiast
Posts: 263
Joined: Sat Oct 09, 2004 6:52 pm
Location: Franklin, PA - USA
Contact:

Re: Can default for a Structure be a null?

Post by GBeebe »

Demivec's approach is probably the better way to go. It is basically the same thing though. Also, I only program in Linux.
Post Reply