C is a medium level general purpose compiled language.
3
votes
1answer
20 views
Style and Suggestions for K&R2 1-18
I just completed K&R (2nd Ed.) ex 1-18. I'm fairly new to C, but have had experience in other languages (Perl, C++, Java, Common Lisp, PHP, etc) and really want to learn the C idiom as opposed to ...
1
vote
0answers
22 views
Portability and “dark corner” gotchas in this “watchdog” program (C, Unix)
The following program is intended to "watchdog" a child process, and all subprocesses the child spawns. The intended behavior is:
A child process (as specified on the command line) is run in a ...
1
vote
3answers
62 views
Improving function that compares two strings
I'm learning C and have made this very simple function to compare two strings. I would like to know how it can be improved:
int match(char* string1, char* string2)
{
size_t i = 0;
while ...
1
vote
0answers
32 views
Singly linked list in C
Here's my version of singly linked list. Any constructive criticism is highly appreciated.
First header file "sll.h":
#ifndef _SLL_H_
#define _SLL_H_
#include <stdio.h>
#include ...
-3
votes
0answers
29 views
How to get computer input? [on hold]
I have only ever played around with scripting style for web development, and only a little. This is one of my assignments for the week, I am wondering how to now get to where the computer/scale would ...
3
votes
2answers
63 views
reading unlimited input strings in C
The idea is to write a function that reads string of unknown size from stdin, keep it in dynamically allocated array of unknown size and return copy of that array.
Is this the right approach? Could I ...
0
votes
1answer
22 views
Hardware interrupts for Nios 2
The program is behaving like expected, but I think something better can be done since I'm a novice C programmer. I suspect that the interrupt handler can use some other way of calling the hardware ...
1
vote
0answers
38 views
popen with array of arguments
It seems like there's no good library function I can use for popen with array of arguments instead of single shell-interpreted shell. So I implemented my own one: c h.
Here is the code:
popen_arr.h
...
0
votes
1answer
60 views
Modifying a string by passing a pointer to a void function [on hold]
Beginner here. I have two questions:
Please explain the relationship between pointers and arrays? In this tutorial, they change int c by changing *r in the function. How is that possible?
Also, ...
2
votes
1answer
73 views
Please review my solution to Project Euler program 1
Can someone give me suggestions on how to improve this program?
This is the link to the problem statement.
#include <stdio.h>
main()
{
int S, s3,s2,S1;
int a=333,b=3,c=999, i=5,j=995, ...
2
votes
1answer
78 views
Fastest StringToLowerCase()
I want to optimize the following code:
#ifdef USE_SSE
#define STRING_PREFETCH_TBL(ptr) \
_mm_prefetch(ptr, _MM_HINT_T0); \
_mm_prefetch(ptr+64, _MM_HINT_T0); \
...
1
vote
3answers
178 views
Is this C code optimised?
char* Migrated[]={"026","109"};
int isMigrated(char* codeB)
{
int i;
int n=sizeof(Migrated)/sizeof(*Migrated);
for(i=0;i<n;i++)
{
if(strcmp(Migrated[i],codeB)==0)
...
0
votes
2answers
63 views
Please review polling for Nios 2
The program listens to the Altera FPGA DE2 board's keys that can start and stop and reset a counter:
#include <stdio.h>
#include "system.h"
#include "altera_avalon_pio_regs.h"
extern void ...
2
votes
0answers
30 views
Extract location from HTTP socket
I have the following HTTP reply saved in a local file called source.txt:
HTTP/1.1 301 Moved
Connection: close
Content-length: 111
Location: https://11.12.13.14:81/
Content-type: text/html; ...
0
votes
1answer
47 views
Wildcard search in C version 2
A few days ago, I posted my version of a wildcard search algorithm, which you can see here: Wildcard search in C
Today I'm showing version 2. I didn't want to make it in the same post because the ...
0
votes
1answer
49 views
Linked list in C [closed]
I am a newbie to C. I've just wrote a linked list program, compiled it, and received a segmentation error. I have no clue where it went wrong. Can somebody help me?
# include <stdio.h>
...
4
votes
3answers
98 views
How can this integer-to-hex string code be improved?
I am using this as a learning exercise primarily and want to ensure my code is optimal. The main thing is that it is reliable and I am made aware of any flaws.
Could it be made more efficient? More ...
4
votes
3answers
128 views
Simple Linked List implimentation C
I am learning C and tried to make a basic singly linked list. I am looking for some feedback on my implementation. I have implemented deletion and reversal functions. Be hard on me I am trying to ...
1
vote
0answers
35 views
Lotto system verificator [closed]
Is there a faster way to check whether a given lotto system gives a victory? Namely, one chooses seven numbers from the set {1,2,...,39} and wins if it has at least 4 correct numbers.
#include ...
1
vote
1answer
58 views
Maximum Subarray Problem - Iterative O(n) algorithm
I implemented an iterative O(n) algorithm for solving the maximum sub-array problem. I would like a general review for it.
Here the max_subarray is the main function and the ones which are static are ...
1
vote
0answers
27 views
Maximum Subarray Problem - Recursive O(n lg n) algorithm
I have implemented a recursive O(n lg n) algorithm for solving the maximum sub-array problem. I would like a general review for it.
Here the max_subarray is the main function and the one which is ...
3
votes
1answer
61 views
Does this C binary search tree have memory leaks?
I am new to C, so I have been trying to get practice doing some coding.
Usually I use languages like Java, python, ruby, etc: Garbage collected languages. So manually cleaning up is new to me and I ...
2
votes
1answer
36 views
Improving Trie Implementation
This is an implementation of a trie, that I will be using on a code editor to parse keywords from a given programming language. I decided to use a trie when I asked this question, and the ...
3
votes
4answers
117 views
Wildcard search in C
In the effort to improve my C knowledge, I tried creating a wildcard search function:
bool wildcard(char *value, char *wcard) {
size_t vsize = strlen(value);
size_t wsize = strlen(wcard);
...
0
votes
2answers
53 views
Basic cons cells in C
So, I'm diving into C and decided to create cons cells. I'd like some thoughts on style and correctness. There are no warnings with -Wall -g and Valgrind doesn't complain about anything.
I'm really ...
2
votes
1answer
72 views
How do you like to returning void? [closed]
This question is related to C, C++, Objective-C and maybe some others. Here is source code example:
void SampleFunc(Data * data)
{
Error * error;
some_parsing_1(data, &error);
if ...
3
votes
0answers
69 views
How can this simple coroutine implementation be improved?
This is a quick and dirty implementation of coroutines that implements yield by saving and restoring the stack to and from the heap. Here's an earlier version, which does the most naive possible ...
1
vote
0answers
77 views
Hash Table: adding and finding functions
I made this function to teach myself about hash tables. Any ideas? I commented out free(addition) because it was giving me a compiler error, but I need to figure out how to deallocate the memory.
...
0
votes
1answer
59 views
Working C code dealing with lists: problem with memory leak
I have written a working code that generates a simple linked list of dates and associated holidays. I realize that I have a problem with memory leaking as I copy lists repeatedly, so I wrote a ...
0
votes
0answers
52 views
Inky pinky ponky game / Josephus problem
I have written below code for the famous Josephus problem, or inky pinky ponky game, or many other names. Does this looks good or is there any loophole if anyone can highlight. I shall be looking ...
3
votes
1answer
58 views
URL percent encoding function
I wrote a custom URL encoding function and I'd like to run it past a few other experienced C developers. I have tested it on a few strings, and it has worked on all of them.
This is to be run on ...
2
votes
0answers
56 views
General Feedback [closed]
New to C programming as I'm trying to learn as much on my own before taking it in school. Found this on SOF and have been plugging away fixing any noticeable issues. I have it running now but would ...
1
vote
3answers
148 views
Attempting C code clean-up
I've been trying to learn C this summer in preparation for a course I will be taking come fall. I found this on SO and through comments and user help improved it quite a bit. As I'm a newb any more ...
3
votes
2answers
135 views
Maxmin algorithm implementation review
I'm currently taking an algorithm course in college. To find the maximum and minimum elements in an array, I'm using a divide and conquer algorithm. Please offer suggestions for this code snippet. I ...
3
votes
5answers
121 views
How to improve this C library for strings?
I'm working on a C library which attempts to bring some of the Python string functions over to C.
https://github.com/shrimpboyho/octo
This is where my full source is located. But the majority of ...
2
votes
1answer
59 views
Review internet connection pinging method
This is some C code I have to simply test the internet connection. Any comments/tips on efficiency and refactoring this program down would be greatly appreciated.
int testConnection(void)
{
int ...
2
votes
4answers
166 views
Review working C code — linked list
I am new to C and have written this code just to teach myself how to work with linked lists and was hoping someone could review it and give me any comments or tips for efficiency, etc. The code just ...
1
vote
1answer
43 views
Freeing allocated memory: am I missing something? [closed]
If this question should be on stackoverflow instead, please point it out. I'll put it up there.
struct {
FILE * fd;
hdr_t file_header;
body_t file_body;
} fileinfo;
The above is ...
1
vote
1answer
63 views
What's the time complexity of the following C code? [closed]
I'm supposed to write a function that prints a given binary tree level by level from bottom to top in time complexity of Θ(n), does the following recursion follow the guidelines of my assignment?
...
3
votes
4answers
125 views
Code review for Image Flip algorithm in C
This is the algorithm I am using to flip an image in-place. Thought so far it works perfect, I would like if any one can spot any potential problems or give any points on improvement and optimization. ...
2
votes
2answers
76 views
Thread safe message queue without mutex in C
I'm trying to write a message queue implementation
so my threads can exchange data.
To keep things simple, the queue is only responsible for
sending pointers to memory. Only one thread may send ...
2
votes
3answers
194 views
Review implementation of stack by using pointers in C
After I had my code for stack implementation by array reviewed I wrote stack implementation by using pointers. Here's my code. Any suggestions for improvement are welcome.
I'll be adding updated ...
3
votes
3answers
182 views
Random String generator in C
I created this small function just to practice C code.
It's a simple random string generator.
#include <string.h>
#include <time.h>
char *randstring(int length) {
static int ...
2
votes
2answers
311 views
Review implementation of stack by using array in C
This question has become long after many updates. Click here to go down.
I have implemented stack using arrays in C. Please give me suggestions on how to improve it. The purpose of writing it is ...
3
votes
2answers
90 views
Recursive a+b function
I am practising recursion and have written the small code for summing a+b as below:
#include <stdio.h>
int b=6;
static int cnt;
void main(void)
{
int a=9,sum;
sum=succ(a);
...
1
vote
1answer
64 views
Random integer generation in given range
Can my rand_int function be improved?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int rand_int(int a,int b)
{
//The basic function here is like (rand() % range)+ ...
1
vote
1answer
78 views
More efficient way to retrieve data from JSON
I am using the jsmn JSON parser (source code) to get some text from a JSON. jsmn stores the data in tokens, but the tokens do not hold any data, they just point to the token boundaries in JSON string ...
2
votes
2answers
108 views
Please review my solution to project euler problem 1
I solved project euler's problem 1 in C.
Can someone give me suggestions on how to improve this solution? Also, please tell me whether or not my programming style is good.
#include<stdio.h>
...
1
vote
1answer
27 views
Efficient way of printing a range of numbers using configurable worker threads
I have written a code to print a range of numbers using multi-threaded programming , the code takes in the number of threads to spawn as input .The code is working fine and giving the expected results ...
0
votes
1answer
52 views
Build a binary-heap from an array, which method is more efficient and why
I am learning about heaps, and I have found two ways of building them from a given array: I am trying to build up a MAX Heap.
1. Top-down approach
Here I check for every element to see if it is at ...