### Author Topic: Pointers in C vs PB  (Read 4052 times)

0 Members and 1 Guest are viewing this topic.

#### Olav Bergesen

• Newbie
• Posts: 14
• User-Rate: +0/-0
##### Pointers in C vs PB
« on: March 01, 2016, 11:12:29 AM »
Hello,

The code below is a PB version of C/C++ code found at http://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/

The problem is that the code below produce the same string for each call to permute.
Could someone shed some light on what is wrong? Thank you!
--
Code: [Select]
`#Compiler PBCC 6.04#Compile Exe#Dim All#Register None'%Debug = 1&'#Include Once "Debug.bi"%USEMACROS = 1#Include Once "Win32Api.inc"'------------------------------------------------------------------------------Function PBMain () As Long  Local s As StringZ * 4  s = "ABC"  permute(s, 0, Len(s)-1)  ? "Press a key to end the program..."  WaitKey\$End Function'----------------------------------------------------------------------------'/* Function to print permutations of string'   This function takes three parameters:'   1. String'   2. Starting index of the string'   3. Ending index of the string. */Sub permute(a As StringZ,ByVal n As long,ByVal r As long)  Local s1 As StringZ Ptr ' * 4  Local s2 As StringZ Ptr  ' * 4  Local i As Byte  s1 = VarPtr(a)  s2 = s1  If n = r Then     ? @s1   Else   For i = n To r     s1 = s1+n     s2 = s2+i     Swap s1,s2      Call permute(a,n+1,r)     s1 = s2+n     s2 = s2+i     Swap s1,s2 '; //backtrack   Next  End IfEnd Sub`

#### Olav Bergesen

• Newbie
• Posts: 14
• User-Rate: +0/-0
##### Re: Pointers in C vs PB
« Reply #1 on: March 01, 2016, 08:42:55 PM »
Works now. Has anyone any clue to why the previous version didn't produce correct result?
--
Code: [Select]
`#Compiler PBCC 6.04#Compile Exe#Dim All#Register None%Debug = 1&'#Include Once "Debug.bi"%USEMACROS = 1#Include Once "Win32Api.inc"'------------------------------------------------------------------------------Function PBMain () As Long  Local s As String  s = "ABC"  permute(s, 0, Len(s)-1)  ? "Please, press a key to end the program..."  WaitKey\$End Function'/* Function to print permutations of string'   This function takes three parameters:'   1. String'   2. Starting index of the string'   3. Ending index of the string. */Sub permute(ByVal a As String,ByVal n As Long,ByVal r As Long)  Local i As Long  Local s1 As Byte Ptr  s1 = StrPtr(a)  If n = r Then     ? a  Else   For i = n To r     Swap @s1[n],@s1[i] 'a+n,a+i     Call permute(a,n+1,r)     Swap @s1[n],@s1[i] 'a+n,a+i) '; //backtrack   Next  End IfEnd Sub`

#### Frederick J. Harris

• Hero Member
• Posts: 914
• User-Rate: +16/-0
##### Re: Pointers in C vs PB
« Reply #2 on: March 05, 2016, 01:34:44 AM »
I'll look at it in just a bit Olav.

#### Frederick J. Harris

• Hero Member
• Posts: 914
• User-Rate: +16/-0
##### Re: Pointers in C vs PB
« Reply #3 on: March 05, 2016, 03:29:32 PM »
Well, this is a bit tricky...

Code: [Select]
`void swap(char* x, char* y)    /* Function to swap values at two pointers */{    char temp;    temp = *x;    *x = *y;    *y = temp;}`
The function parameters are two char* (character pointers), which can point to arrays of chars containing from zero to 2^31 characters.  But note the local char temp variable is not a pointer to anything, but simply is a C variable type capable of only containing one single character.  When they make this assignment...

Code: [Select]
`temp = *x;`
...temp will only contain the 1st character of whatever char pointer variable x points to, e.g., if x points to this string...

Code: [Select]
`"ABC"`
...then temp will contain just the 'A' character and nothing more.

I took a quick look at your 2nd version which you say works, and I see your are using pointer offset notation to extract just the single character.  The closest thing in PowerBASIC to the C/C++ char is the Byte.