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

Function visible even if not included in C

I have defined function A in main.c file. I have created three libraries which use the function A without importing anything. The code works but I have only one warning: implicit declaration of function 'A' [-Wimplicit-function-declaration]. How is ...
more »

2017-06-16 11:06 (3) Answers

malloc_usable_size() returns the wrong size

I want to know the size allocated by malloc. I have written the source code below. test.c #include <stdio.h> #include <stdint.h> #include <malloc.h> void main(void) { uint8_t *test; test = (uint8_t *)malloc(sizeof(uint8_t...
more »

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