So I did a bit of C programming a while ago and basically forgot it all lol but anyways I started doing this “C Refresher” thing I found online and was following this binary search tree example kinda loosely and ran into an error. Once I compile it and run it, it says “Error: Can’t Open Display”. I am running this code on some kinda Linux server in school. Anyways, here’s the code:
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
typedef struct Node {
int value;
struct Node *left;
struct Node *right;
} Node;
void add (Node *node, int value)
{
if (value < node->value) {
//left side
if (node->left == NULL) {
Node *newNode = malloc(sizeof(Node));
newNode->value = value;
newNode->left = NULL;
newNode->right = NULL;
node->left = newNode;
} else {
add(node->left, value);
}
} else {
//right side
if (node->right == NULL) {
Node *newNode = malloc(sizeof(Node));
newNode->value = value;
newNode->left = NULL;
newNode->right = NULL;
node->right = newNode;
} else {
add(node->right, value);
}
}
}
int search(Node *node, int value)
{
if (node == NULL) {
return FALSE;
} else if (node->value == value) {
return TRUE;
} else {
if (value < node->value) {
return search(node->left, value);
} else {
return search(node->right, value);
}
}
}
int main (int argc, char *argv[])
{
Node root;
root.value = 23;
root.left = NULL;
root.right = NULL;
add(&root, 5);
add(&root, 50);
add(&root, 8);
add(&root, 2);
add(&root, 34);
if (search(&root, 23)) {
printf("23 lives in the tree.\n");
} else {
printf("23 does not live in the tree.\n");
}
if (search(&root, 42)) {
printf("42 lives in the tree.\n");
} else {
printf("42 does not live in the tree.\n");
}
return 0;
}
The code might seem long but it is actually pretty basic. I think I coulda cut some of the code out before pasting it here but I figured I would leave everything in in case I took out something vital to the problem.
Also I thought it might have something to do with the Node thing so in my main method I put a quick printf("hi"); before the Node root; to see if that would make a difference but it still gave me the same error. And I have another program on my account on this school server and that program has some printf statements and it runs just fine.
I tried Googling the problem but all this weird Linux threads came up and I couldn’t really understand it. My computer is Windows but I did all this coding in a program called emacs that I got to through a program called PuTTY that lets me connect to the school Linux server.
Also I compiled it with gcc -o tree tree.c.
Sorry for all the writing, I was just trying to give as much information as possible. Thanks to anyone who can help!
Are you invoking it like “./tree” or just “tree”. It looks like you are trying to run a gui application over ssh. To see which application try connecting with “ssh -XY HOST” if you are on a linux machine. Then you should see an application launching.