Skip to content

Invalid Go code when translating pointer-to-pointer to struct #938

Description

@chisa22

c2go incorrectly translates a C pointer-to-pointer to a struct type.
In C, struct Point **pptr = &ptr1; declares pptr as a pointer to ptr1, where ptr1 has type struct Point *. Therefore, pptr should have two levels of indirection.
However, the generated Go code declares pptr as *Point instead of **Point. This loses one pointer level and causes type errors in both the declaration and later dereference.
To Reproduce

#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main(void) {
    struct Point p1 = {3, 7};
    struct Point *ptr1 = &p1;
    struct Point **pptr = &ptr1;

    printf("%d\n", (*pptr)->x);
    return 0;
}

Generated Go Code

type Point struct {
        x int32
        y int32
}

func main() {
        var p1 Point = Point{int32(3), int32(7)}
        var ptr1 *Point = &p1
        var pptr *Point = &ptr1
        noarch.Printf((&[]byte("%d\n\x00")[0]), (*(*pptr)).x)
        return
}

Observed Behavior
The generated Go code fails to compile:

# command-line-arguments
./test.go:171:20: cannot use &ptr1 (value of type **Point) as *Point value in variable declaration
./test.go:172:44: invalid operation: cannot indirect (*pptr) (variable of type Point)

The main issue is that struct Point ** should be translated as **Point, not *Point.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions