Segmentation fault - Core dumped in the code

This code is used to get n dates and sort them in the ascending order. I think the getdates function is not working properly. main function implementation: #include <stdio.h> #define max 30 int leapyear(int year);/*to check whether it's leap...
more »

2017-03-21 23:03 (1) Answers

Error condition, how to implement it?

So what my codes does at the moment is quite simple. It receives an input like this: 5 4 1 2 2 3 3 4 4 5 The first line of the input represents the number of Ids and the number of relations. The following line are those relations. The program sort...
more »

2017-03-21 21:03 (0) Answers

"ifdef" in PowerPC Assembly

I want to make a ifdef condition in assembly code similar to the Pre-processor in C. for example : ifdef UNIT_TEST b somewhere else b somewhere else I'm using e200z6 PowerPC core. ...
more »

2017-03-21 16:03 (1) Answers

Is checking the return value of printf important?

In one of my University Projects, I got points off and feedback from my professor saying I didn't handle printf errors. In English --> / * ### FB: Error handling printf () is missing * /     /* ### FB: Fehlerbehandlung printf() fehlt */ prin...
more »

2017-03-21 15:03 (5) Answers

Writing a make file for GLFW3

i'm writing an emulator program and I need a graphics library. I have 4 files, the graphics library GLWF3 is installed in my includes folder. Im using MacOs Yosemite. I can't figure out how to get the makefile working though to include the glfw3 libr...
more »

2017-03-21 05:03 (1) Answers

How to affect the same copy of a struct in C?

I'm fairly new to C, so please bear with me :) I'm trying to learn the language, and I have found trouble when trying to make changes to elements of the same struct. Consider the following code: #include <stdio.h> struct aStruct{ int someN...
more »

2017-03-21 04:03 (1) Answers

C function not returning value

I've written a program for a class and am having a problem. The purpose of the program is to read a set of values into an array, calculate the average, and then find how many elements in that array are larger than that average. The function prototype...
more »

2017-03-21 03:03 (3) Answers

Pointers with If statements in C

I am writing a text-based game in C. At the start of the program, it prompts you if you want to start or end the game. However, when I type end or start, it brings up a "Segmentation Fault" error. Here is my code: #include <stdio.h> #include &...
more »

2017-03-21 03:03 (2) Answers

scanf test failing inside a function in C

I'm trying to do a program with a simple game for a user to guess the number. My code is below: #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX 30 #define TRYING 5 void guessnumber(int, int, int *); int main...
more »

2017-03-20 18:03 (2) Answers

C char array v C char* initialization

The following is accepted as valid c code by gcc version 6.3: char white[] = { 'a', 'b', 'c' }; char blue[] = "abc"; char *red = "abc"; However the following fails: char *green = { 'a', 'b', 'c' }; // gcc error I am sure there is a perfe...
more »

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

volatile and non-volatile bitfields

I'm writing a code for Cortex-M0 CPU and gcc. I've the following structure: struct { volatile unsigned flag1: 1; unsigned flag2: 1; unsigned foo; // something else accessed in main loop } flags; flag1 is read and written from both GPI...
more »

2017-03-20 12:03 (2) Answers

login with Do..while loop in C

I need to make a program that asks for a username and password and if they are both correct continue to a menu. Currently inputting a correct username and wrong password for some reason still prompts me to the menu, however if i input a wrong usernam...
more »

2017-03-20 11:03 (4) Answers

Interview questions about string and char

I faced two questions from a company. The two questions confused me. Can anyone help explain the reason of the answers? Write the result. void Test(void){ char *str = (char *) malloc(100); strcpy(str, “hello”); free(str); if(str != NUL...
more »

2017-03-20 10:03 (3) Answers

pthread_mutex_t deadlock after package update

we are using pthread_mutex_t to protect a shared memory accessed by different processes. After the recent update of glibc ( the code did stop to work, resulting in a deadlock of every invol...
more »

2017-03-20 10:03 (0) Answers

const values in array dimensions

The below code snippet works and compiles in C const int n=10; int main(void) { int a[n]; return 0; } However when array is declared in global scope ,it throws compilation error. const int n=10; int a[n]; int main(void) { return 0; } ...
more »

2017-03-20 08:03 (1) Answers

How safe are local variables?

I am writing a kind of dinky program in c which requires the user to enter a username and password. I know it's not really necessary, but I kind of like thinking about the security side of things. I hash the password before saving it to a file, but i...
more »

2017-03-20 05:03 (0) Answers

Ending child process with return 0

Is return 0 in case 0 the correct choice? In other words, does return 0 call the _exit() function? Or maybe it calls exit(), hence this code is unsafe? #include <errno.h> #include <unistd.h> #include <sys/types.h> #include <sys...
more »

2017-03-20 00:03 (1) Answers

pthread mutex not unlocking on wait

I'm using c posix threads, and i've encountered a problem I don't quite understand and would like some fresh eyes to see what the problem could be. In summary, I'm creating 3 threads which check some program state, signal that they are ready, and th...
more »

2017-03-19 18:03 (1) Answers

fcntl F_GETLK always returns F_UNLCK

I am trying to understand POSIX file-region locks in C. The program below is really simple, sets the lock to F_WRLCK and then gets locks. There is no errors during opening/setting lock. Unfortunatelly it's always returning F_UNLCK. Where is the mista...
more »

2017-03-19 10:03 (1) Answers

Very simple cryptography function in C

I'm trying to create a very simple cryptography function that takes a character array and returns another character array with its value increased by one. Precise explanation: The input is a line of text inserted by the user. I expect that the array...
more »

2017-03-19 08:03 (4) Answers

Why Can I Trust Memory Allocation?

I'm currently taking an introductory CS course that uses C. My textbook seems to imply that a pointer variable still holds the address for memory previously allocated to it, even after free() was called on it -- assume I previously used malloc(). Doe...
more »

2017-03-19 08:03 (5) Answers