Skip to content

Instantly share code, notes, and snippets.

@8dcc
Created March 17, 2022 17:00
Show Gist options
  • Save 8dcc/90898afa73311cb674753896cea79268 to your computer and use it in GitHub Desktop.
Save 8dcc/90898afa73311cb674753896cea79268 to your computer and use it in GitHub Desktop.
Simple way to pass 2d arrays to a C function.
#include <stdio.h>
#define ARRAY_SIZE 10
int test(int* array, int ys, int xs) {
int desiredx = 5;
int desiredy = 3;
array[desiredy * xs + desiredx] = 1;
array[desiredy * ys + desiredx] = 2;
}
int main() {
int test_array[ARRAY_SIZE][ARRAY_SIZE];
// Clear array
for (int y = 0; y < ARRAY_SIZE; y++) {
for (int x = 0; x < ARRAY_SIZE; x++) {
test_array[y][x] = 0;
}
}
// Call the test function
test(&test_array[0][0], ARRAY_SIZE, ARRAY_SIZE);
// Print array
for (int y = 0; y < ARRAY_SIZE; y++) {
for (int x = 0; x < ARRAY_SIZE; x++) {
printf("%d", test_array[y][x]);
}
printf("\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment