Skip to content

Instantly share code, notes, and snippets.

@Skarsnik

Skarsnik/test.c Secret

Created January 2, 2016 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Skarsnik/31a1099ceb0ea595e173 to your computer and use it in GitHub Desktop.
Save Skarsnik/31a1099ceb0ea595e173 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT extern
#endif
struct Point {
int x;
int y;
};
DLLEXPORT void * ReturnSomePointer()
{
return strdup("Got passed back the pointer I returned");
}
DLLEXPORT int CompareSomePointer(void *ptr)
{
int x = strcmp("Got passed back the pointer I returned", ptr) == 0;
free(ptr);
return x;
}
DLLEXPORT void * ReturnNullPointer()
{
return NULL;
}
DLLEXPORT void * TakeTwoPointersToInt(int *ptr1, int *ptr2)
{
return NULL;
}
DLLEXPORT void * TakeCArrayToInt8(int array[]) {
return NULL;
}
DLLEXPORT void NewPoint(struct Point *p)
{
p = malloc(sizeof(struct Point));
p->x = 42;
p->y = 42;
}
DLLEXPORT void NewPointPtr(struct Point **p)
{
printf("NewPointPtr\n");
*p = malloc(sizeof(struct Point));
(*p)->x = 42;
(*p)->y = 42;
}
class Point is repr('CStruct') {
has int32 $.x;
has int32 $.y;
}
sub NewPointPtr(Pointer[Pointer[Point]] is rw) is native("./04-pointers") { * };
my Pointer[Pointer[Point]] $ptr2 .= new;
say "Pass ptr to C";
NewPointPtr($ptr2);
say "Hello";
my Point $p = $ptr2.deref.deref;
ok $p.x == 42 and $p.y == 42;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment