Segmentation fault while executing simple pin state change code

I’m new to beaglebone and to C programming so I may be asking a stupid question but when I try to run the following code it exports gpio44 but gives a segmentation fault error 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 no idea what could be causing this.

If you’re new to programming and the beaglebone, I suggest you stop what you’re doing and go read.

You say this code is supposed to be opening /changing whatever on gpio44, but clearly in your code it is opening gpio68.

I’m not sure where you found that code, but its a mess. Anyway look up Derek Molloy on youtube and watch his videos and / or read his blog.