This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cc test.c -o test | |
my_var : 15 | |
my_ptr : 15 | |
my_ptr_ptr : 15 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
// struct with a common interface for varying data types | |
struct poly_struct | |
{ | |
void (*value)(); | |
void (*print)(struct poly_struct); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void ptr_sample() | |
{ | |
int my_var = 15; | |
int *my_ptr = &my_var; | |
int **my_ptr_ptr = &my_ptr; | |
printf("my_var : %i\n", my_var); | |
printf("my_ptr : %i\n", *my_ptr); | |
printf("my_ptr_ptr : %i\n", **my_ptr_ptr); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main(int argc, char const *argv[]) | |
{ | |
struct jintArray jvm_array; | |
jvm_array = create_jintArray(5); | |
struct env_type env_struct = create_env_structure(); | |
struct env_type *env_ptr = &env_struct; | |
struct env_type **env = &env_ptr; | |
int *native_array; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct env_type create_env_structure() | |
{ | |
struct env_type env_struct, *env_ptr; | |
env_struct.GetIntArrayElements = &GetIntArrayElementsFunc; | |
env_struct.ReleaseIntArrayElements = &ReleaseIntArrayElementsFunc; | |
return env_struct; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct jintArray create_jintArray(int size) | |
{ | |
struct jintArray jvm_array; | |
jvm_array.size = size; | |
jvm_array.elements = malloc(size * sizeof(int)); | |
for (int i = 0; i < (jvm_array.size); i ++){ | |
jvm_array.elements[i] = i; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int *ReleaseIntArrayElementsFunc(struct jintArray jvm_array, int *native_array) | |
{ | |
free(jvm_array.elements); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct env_type | |
{ | |
int *(*GetIntArrayElements)(struct jintArray); | |
int *(*ReleaseIntArrayElements)(struct jintArray, int *native_array); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int *GetIntArrayElementsFunc(struct jintArray jvm_array) | |
{ | |
int size = jvm_array.size; | |
int *native_array = malloc(size * sizeof(int)); | |
for (int i = 0; i < size; i ++){ | |
native_array[i] = jvm_array.elements[i]; | |
} | |
return native_array; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct jintArray | |
{ | |
int size; | |
int *elements; | |
}; |