Risks of adding uint to enum in function call

I have a function that returns a float number: float function(enum value) I then have an enum typedef enum { a = 0, b, c } myenum; I want to do the following: function(a+1); And I wonder if there are any risks other than the risk...
more »

2017-06-20 15:06 (2) Answers

Exchange sort algorithm in c using pointers

I am trying to write an exchange sort in c using pointers, NOT indices; I do not understand why this code does not work: void sort(int a[], int n) { int *i, *j, *temp; int * const end = a + n; for(i = a; i < end - 1; i++) for(j = i + ...
more »

2017-06-19 19:06 (1) Answers

Why malloc can't work with strcpy?

char * removeChar(char * str, char c){ int len = strlen(str); int i = 0; int j = 0; char * copy = malloc(sizeof(char) * (len + 1)); while(i < len){ if(str[i] != c){ copy[j] = str[i]; j++; ...
more »

2017-06-19 17:06 (4) Answers

Why does the following print what it does?

typedef unsigned char byte; unsigned int nines = 999; byte * ptr = (byte *) &nines; printf ("%x\n",nines); printf ("%x\n",nines * 0x10); printf ("%d\n",ptr[0]); printf ("%d\n",ptr[1]); printf ("%d\n",ptr[2]); printf ("%d\n",ptr[3]); Output: ...
more »

2017-06-18 18:06 (2) Answers

Pointers with Arrays in C

I was just practicing some coding with pointers and arrays, and I stumbled upon this code here. int main() { printf("Pointer arrays\n\n"); int x; int arr[3] = {1,2,3}; int* ptr = arr; int size = sizeof(arr)/sizeof(arr[0]); for(x = 0...
more »

2017-06-18 10:06 (2) Answers

Why is my C Code miscalculating

I'm trying to write a formula to calculate the volume of a sphere with user inputting the radius. The formula is V = (4/3)PI r*r*r. I can't figure out why my code is just saying the volume is 1 no matter what the input is. Here is what I'm using: #i...
more »

2017-06-18 09:06 (3) Answers

Inconsistent runtimes with pthreads

My multithreaded C program runs the following routine : #define NUM_LOOP 500000000 long long sum = 0; void* add_offset(void *n){ int offset = *(int*)n; for(int i = 0; i<NUM_LOOP; i++) sum += offset; pthread_exit(NULL); }...
more »

2017-06-18 08:06 (2) Answers

macOS Cocoa app: Crash with unspecified error

I have Status Bar Cocoa App that provides GUI with general information and preferences windows for my background running server. This server is implemented in C programming language and the only work it is doing now is simple echo to client. Applic...
more »

2017-06-18 00:06 (0) Answers

Size of pointers and dynamic arrays

I'm relatively new to C, and I've been messing around with pointers to an int array to help solidify my understanding. Here is some code I typed up that confused me: #include <stdio.h> int main(int argc, char **argv) { int sizeOfInt = si...
more »

2017-06-17 18:06 (3) Answers

Removing spaces from strings

I tried to write a function that gets a string and creates a new string but without multiple spaces (leaving only 1 space between words). So far I wrote this, but for some reason it crashs and the debugger shows nothing. I also don't know where do ...
more »

2017-06-17 15:06 (4) Answers

Why is this pointer variable assignment allowed?

While debugging a program of mine, I stumbled upon a weird behaviour of the gcc compiler. I don't know what's the correct title to describe this, but take a look at the code below. Basically, I had a function which received a void* arg as an argumen...
more »

2017-06-17 12:06 (1) Answers

Problems casting a double into an unsigned char

Why does casting a double 728.3 to an unsigned char produce zero? 728 is 0x2D8, so shouldn't w be 0xD8 (216)? int w = (unsigned char)728.3; int x = (int)728.3; int y = (int)(unsigned char)728.3; int z = (unsigned char)(int)728.3; printf( "%i %i %i ...
more »

2017-06-16 19:06 (2) Answers

How to open physical disk in EDK2 tianocore?

I am a beginner at EDK2 tianocore. I have EFI Shell and I want to open current USB with EFI Shell as physical disk to read and write blocks. In WinAPI I know CreateFile function with argument like "\\.\D:", ReadFile and WriteFile. What is the analo...
more »

2017-06-16 18:06 (0) Answers

C, Structure writes only 4 characters

im stuck in writing my code with a problem I cant just deal with. I have to write data from a txt file to structures. The code looks like this: typedef struct uzytkownik_ { char *imie; char *nazwisko; char *oprogramowanie; char *ip; ...
more »

2017-06-16 14:06 (1) Answers

Unsure how strcpy works in this situation

I am very new to C and working through a project as a way to help motivate me to learn. I was having a hard time getting the output from an sh popen. After much searching and hours of trial and i stumbled across a very old post where they used strcp...
more »

2017-06-16 12:06 (2) Answers