Segmentation fault in simple C code

I’m new to beaglebone and C programming and this question may be stupid but when I try to run the following code it exports gpio44 but after that it gives segmentation fault and stops.


#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

void export_pin(char pin_n[]);
void unexport_pin(char pin_n[]);
void set_pin(char pin_n[], char pin_val[]);

int main() {
set_pin(“44”, “1”);
return 0;

void set_pin(char pin_n[], char pin_val[]) {

FILE *direction = 0;
FILE *value = 0;

char pin_dir[] = “out”;

direction = fopen("/sys/class/gpio/gpio68/direction", “w”);

fwrite(pin_dir, 1, sizeof(pin_dir), direction);


value = fopen("/sys/class/gpio/gpio68/value", “w”);

fwrite(pin_val, 1, sizeof(pin_val), value);


void export_pin(char pin_n[]) {
FILE *export = NULL;

export = fopen("/sys/class/gpio/export", “w”);

fwrite(pin_n, 1, sizeof(pin_n), export);


void unexport_pin(char pin_n[]) {
FILE *unexport = NULL;

unexport = fopen("/sys/class/gpio/unexport", “w”);

fwrite(pin_n, 1, sizeof(pin_n), unexport);



I have read somewhere that this may be a memory issue but I have no idea how to solve this.

Basically, in your functions, pin_n is passed as an address not a C-string.
The size of an address variable is not the same as the length of a string whose address is stored therein.
Try strlen(pin_n) + 1 in place of sizeof(pin_n)

Also, look at all uses of sizeof. pin_dir and pin_val also have the same problem.