October 21, 2024
Chicago 12, Melborne City, USA
C#

Include an external library in C

I’m attempting to use a C library for an opencourseware course from Harvard. The instructor’s instructions for setting up the external lib can be found here. I am following the instructions specific to ubuntu as I am trying to use this lib on my ubuntu box. I followed the instructions on the page to set

Read More
C#

Static global pointer in struct.h is malloced by a function in struct.c, but it is NULL inside main()

struct.h #ifndef STRUCT_H #define STRUCT_H struct linkedlist { int data; }; static struct linkedlist *head = NULL; void build_linkedlist(); #endif struct.c #include <stdio.h> #include <stdlib.h> #include "struct.h" void build_linkedlist() { head = malloc(sizeof(struct linkedlist)); head->data = 5; printf("inside build_linkedlist(): head = %p\n\n", head); } main.c #include <stdio.h> #include "struct.h" int main() { build_linkedlist(); printf("inside main():

Read More
C#

Write reusable pointer type casting logic in a function or macro

Here is the context of my question. I have this code : typedef enum DType { float32 } DType; typedef struct Array { void *data; DType dtype; ... } Array; The whole idea is to cast the pointer data to the adequate type depending on the value of the field dtype. Although it is possible

Read More
C#

Why printf show false value of an hex number

Code char a; a = 0xf1; printf("%x\n", a); Output fffffff1 printf() show 4 bytes, that exactly we have one byte in a. What is the reason of this misbehavior? How can I correct it? You need to sign in to view this answers

Read More
C#

C, Calculating the distance between two GPS locations?

If I have two GPS locations, say 51.507222, -0.1275 and 48.856667, 2.350833, what formula could I use to calculate the distance between the two? I’ve heard a lot about a haversine formula, but can’t find any information about it, or how to apply it to C. I’ve written the following code, however, it’s very innacurate.

Read More
C#

Anonymous union within struct not in c99?

here is very simplified code of problem I have: enum node_type { t_int, t_double }; struct int_node { int value; }; struct double_node { double value; }; struct node { enum node_type type; union { struct int_node int_n; struct double_node double_n; }; }; int main(void) { struct int_node i; i.value = 10; struct node n;

Read More
C#

C calculator refuses to work when more than 1 “if” statements are added

I made a small C calculator earlier today, and I noticed it worked fine when the only operation it was able to do was addition, however when I tried to add subtraction, multiplication or division, it would simply not give an output, and I don’t know the reason for it as another calculator I had

Read More
C#

Reinterpret cast in C – cannot take the address of an rvalue

I have two structs struct A and struct B with the same memory layout, and I want to cast a return value of type struct A to type struct B, for example, in C++, I can achieve this goal in single line (without any temporary variable) like this: #include <utility> struct Temp { int a;

Read More
C#

c code for solving an augmented matrix of size [N][N+1]

#include <stdio.h> #include <stdlib.h> #include <math.h> void pivot(double **matrix, int n, int row) { double maxElement = fabs(matrix[row][row]); int maxRow = row; for (int i = row + 1; i < n; i++) { if (fabs(matrix[i][row]) > maxElement) { maxElement = fabs(matrix[i][row]); maxRow = i; } } if (maxRow != row) { double *temp =

Read More
C#

How to debug a recursive Sokoban solver, which occasionally runs with wrong data

I am writing a program to solve Sokoban levels – because why not. The initial trigger for writing the program was to show a colleague how a program can be written using the MVC pattern. From there, it evolved. The good: for some warehouses, the solver finds the proper solutions. I know because I created

Read More