Im battling to understand pointers to strings in a function - Please help

112 views
Skip to first unread message

Silver 7331

unread,
May 30, 2020, 3:43:29 PM5/30/20
to golang-nuts
I'm 1 day old in this wonderful language and trying my best to learn.
When I try to build the below code I get the following error/s
./prog.go:66:6: missing function body
./prog.go:66:30: syntax error: unexpected [, expecting comma or )
./prog.go:69:2: syntax error: non-declaration statement outside function body

Go build failed.
I figure it must have something to do with the pointer to the string in the function inx, more specifically sr *string[1000]

Thanks in advance.

code:
package main

import "fmt"


func main() {
theStr := "ASDSFHHTBBKLKKJHHDFGHJKHHHJHHKJKJJKJKsdkejffkjf43889934ythbnv3ouy4ti3ykfjjvwkjhfwiuiu4ui4uiyruuhfufuiwefhfwekllllllfe34t3gphrepgqhg38po3hgvnfh38t98ohgerqoi084rt3ukovpklmnfvb [gj po 5hop p6iy 904254uy 45uy 45iuuy 45uy 24u209386 34tu 09y0 5u5ty9025y  59u934tp33tgprg nbnvkoiojh1234567iklr,fcv7ytgvb rgb 98uhbrtghnoikjnrfgvyhntghbuik,rfvokmrtg9iuj5rtfychntf8cihjng vf8ihjngvfi hnb jikjgdfkjverohgvoreihjgolerkgoehvve rlkvoijinvn o4erugoerutjblkgfbkdjfnveorijgve;djvboerijvbofdn"
 
var (
iWork int
sWork string
iRegister2[1000] uint64
iRegister4[1000] uint64
iRegister8[1000] uint64
iRegister16[1000] uint64
iRegister32[1000] uint64
iRegister64[1000] uint64
sRegister2[1000][2] string
sRegister4[1000][4] string
sRegister8[1000][8] string
sRegister16[1000][16] string
sRegister32[1000][32] string
sRegister64[1000][64] string
iInx2 uint32
iInx4 uint32
iInx8 uint32
iInx16 uint32
iInx32 uint32
iInx64 uint32
)
iWork = 0
for iWork = 0; iWork < len(theStr); iWork = iWork + 2{
if (iWork + 2) < len(theStr){
sWork = theStr[iWork:iWork + 1]
inx(sWork, &sRegister2, &iRegister2, &iInx2)
}
if (iWork + 4) % 4 == 0 && (iWork + 4) < len(theStr){
sWork = theStr[iWork:iWork + 3]
inx(sWork, &sRegister4, &iRegister4, &iInx4)
}
if ((iWork + 8) % 8) == 0 && (iWork + 8) < len(theStr){
sWork = theStr[iWork:iWork + 7]
inx(sWork, &sRegister8, &iRegister8, &iInx8)
}
if ((iWork + 16) % 16) == 0 && (iWork + 16) < len(theStr){
sWork = theStr[iWork:iWork + 15]
inx(sWork, &sRegister16, &iRegister16, &iInx16)
}
if ((iWork + 32) % 32) == 0 && (iWork + 32) < len(theStr){
sWork = theStr[iWork:iWork + 31]
inx(sWork, &sRegister32, &iRegister32, &iInx32)
}
if ((iWork + 64) % 64) == 0 && (iWork + 64) < len(theStr){
sWork = theStr[iWork:iWork + 63]
inx(sWork, &sRegister64, &iRegister64, &iInx64)
}
}
fmt.Printf("2 char array = %d\n", iInx2)
fmt.Printf("4 char array = %d\n", iInx4)
fmt.Printf("8 char array = %d\n", iInx8)
fmt.Printf("16 char array = %d\n", iInx16)
fmt.Printf("32 char array = %d\n", iInx32)
fmt.Printf("64 char array = %d\n", iInx64)
}

func inx(s string, sr *string[1000], ir *uint64, ii *uint32) {
var i int

for i = 0; i < *ii && (*sr)[i] != s; i++ {
}
if i >= *ii {
(*sr)[i] = append((*sr)[i],s)
*ii = i
(*ir)[i]++
}
}


Ian Lance Taylor

unread,
May 30, 2020, 4:06:46 PM5/30/20
to Silver 7331, golang-nuts
The compiler error message is not the greatest, but the problem is
that you wrote "string[1000]" where you presumably meant to write
"[1000]string".

Ian

Saied Seghatoleslami

unread,
May 30, 2020, 4:13:57 PM5/30/20
to golang-nuts
1. Check what is *string[1000].  Maybe you mean *[1000]string
2. Also, it looks like you are passing [1000][2]string etc. to idx, they are of different types.  Once you fix that, below are the errors that you get
3. Finally, why use fixed size arrays, that is into idiomatic go.  Using slices is.

Enter code here..../test.go:34:15: cannot use &sRegister2 (type *[1000][2]string) as type *[1000]string in argument to inx

./test.go:34:28: cannot use &iRegister2 (type *[1000]uint64) as type *uint64 in argument to inx

./test.go:38:15: cannot use &sRegister4 (type *[1000][4]string) as type *[1000]string in argument to inx

./test.go:38:28: cannot use &iRegister4 (type *[1000]uint64) as type *uint64 in argument to inx

./test.go:42:15: cannot use &sRegister8 (type *[1000][8]string) as type *[1000]string in argument to inx

./test.go:42:28: cannot use &iRegister8 (type *[1000]uint64) as type *uint64 in argument to inx

./test.go:46:15: cannot use &sRegister16 (type *[1000][16]string) as type *[1000]string in argument to inx

./test.go:46:29: cannot use &iRegister16 (type *[1000]uint64) as type *uint64 in argument to inx

./test.go:50:15: cannot use &sRegister32 (type *[1000][32]string) as type *[1000]string in argument to inx

./test.go:50:29: cannot use &iRegister32 (type *[1000]uint64) as type *uint64 in argument to inx

Silver 7331

unread,
May 31, 2020, 3:07:57 AM5/31/20
to golang-nuts
Thanks all for your help.
I got it to work like this:
package main

import "fmt"

var (
iWork int
sWork string
)
func main() {
theStr := "ASDSFHHTBBKLKKJHHDFGHJKHHHJHHKJKJJKJKsdkejffkjf43889934ythbnv3ouy4ti3ykfjjvwkjhfwiuiu4ui4uiyruuhfufuiwefhfwekllllllfe34t3gphrepgqhg38po3hgvnfh38t98ohgerqoi084rt3ukovpklmnfvb [gj po 5hop p6iy 904254uy 45uy 45iuuy 45uy 24u209386 34tu 09y0 5u5ty9025y  59u934tp33tgprg nbnvkoiojh1234567iklr,fcv7ytgvb rgb 98uhbrtghnoikjnrfgvyhntghbuik,rfvokmrtg9iuj5rtfychntf8cihjng vf8ihjngvfi hnb jikjgdfkjverohgvoreihjgolerkgoehvve rlkvoijinvn o4erugoerutjblkgfbkdjfnveorijgve;djvboerijvbofdn"
iRegister2 := make([]uint64, 1000, 10000)
sRegister2 := make([]string, 1000, 10000)
iRegister4 := make([]uint64, 1000, 10000)
sRegister4 := make([]string, 1000, 10000)
iRegister8 := make([]uint64, 1000, 10000)
sRegister8 := make([]string, 1000, 10000)
iRegister16 := make([]uint64, 1000, 10000)
sRegister16 := make([]string, 1000, 10000)
iRegister32 := make([]uint64, 1000, 10000)
sRegister32 := make([]string, 1000, 10000)
iRegister64 := make([]uint64, 1000, 10000)
sRegister64 := make([]string, 1000, 10000)
 
for iWork = 1; iWork <= len(theStr); iWork = iWork + 2{
if (iWork + 1) < len(theStr){
sWork = theStr[iWork - 1:iWork + 1]
inx(&sWork, &iRegister2, &sRegister2)
}
if ((iWork + 3) < len(theStr) && (iWork + 3) % 4 == 0){
sWork = theStr[iWork - 1:iWork + 3]
inx(&sWork, &iRegister4, &sRegister4)
}
if ((iWork + 7) < len(theStr) && (iWork + 7) % 8 == 0){
sWork = theStr[iWork - 1:iWork + 7]
inx(&sWork, &iRegister8, &sRegister8)
}
if ((iWork + 15) < len(theStr) && (iWork + 15) % 16 == 0){
sWork = theStr[iWork - 1:iWork + 15]
inx(&sWork, &iRegister16, &sRegister16)
}
if ((iWork + 31) < len(theStr) && (iWork + 31) % 32 == 0){
sWork = theStr[iWork - 1:iWork + 31]
inx(&sWork, &iRegister32, &sRegister32)
}
if ((iWork + 63) < len(theStr) && (iWork + 63) % 64 == 0){
sWork = theStr[iWork - 1:iWork + 63]
inx(&sWork, &iRegister64, &sRegister64)
}
}
}
func inx(s *string, ir *[]uint64, sr *[]string){
var i int
fmt.Printf("The Value is : %s", *s)
for i = 0; ((*ir)[i] != 0 && (*sr)[i] != *s); i++ {
}
if (*sr)[i] != *s {
(*sr)[i] = *s
}
(*ir)[i]++
fmt.Printf("%s = %d\n", *s, (*ir)[i])

derek kenney

unread,
May 31, 2020, 5:57:07 PM5/31/20
to golang-nuts
I cleaned up the code a bit so that your function calls are working. The results are still not coming our correctly. Take another pass at the logic for inx(), and you should be able to get your solution working. Use slices instead of fixed length arrays. Also, you don't need to use reference pointers with primitive types. Hope this helps.
Reply all
Reply to author
Forward
0 new messages