Skip to content

Instantly share code, notes, and snippets.

View radiofreejohn's full-sized avatar
🤓

John radiofreejohn

🤓
  • Goldsky
  • Raleigh, NC
View GitHub Profile
@radiofreejohn
radiofreejohn / range.c
Created March 23, 2011 04:27
generate a range of integer values range(start,end) - may be neg to pos and vice versa
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int *range(int, int);
int *range(int start, int end)
{
int size = (int) fabs(end-start)+1;
int *values = malloc(size * sizeof(int));
@radiofreejohn
radiofreejohn / function-pointers.c
Created March 18, 2011 23:51
experimenting with function pointers, what a mess...
#include <stdio.h>
void printnnl(char *);
void printnl(char *);
void printgeneric(void (*printfunc)(char *), char *str);
void printgeneric(void (*printfunc)(char *), char *str)
{
(*printfunc)(str);
}
@radiofreejohn
radiofreejohn / kr5-10.c
Created March 18, 2011 08:12
K&R Exercise 5-10, reverse polish notation evaluator, this is rough
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
float polishStack[100];
float *stackPtr;
float tempx, tempy;
float *start;
@radiofreejohn
radiofreejohn / kr5-5-strncat.c
Created March 12, 2011 23:31
K&R Exercise 5-5, strncat
#include <stdio.h>
void strnfeline(char *s, const char *t, int n);
void strnfeline(char *s, const char *t, int n)
{
//this is bad if s is not terminated
while(*++s)
;
while(n--)
@radiofreejohn
radiofreejohn / kr5-4.c
Created March 12, 2011 22:56
K&R exercise 5-4, strend, find string at end of another string
#include <stdio.h>
int strend(char *s, char *t);
int strend(char *s, char *t)
{
//save pointer to the first element of t
char *start = t;
//move to the end of each string
while (*++s)
@radiofreejohn
radiofreejohn / kr5-3.c
Created March 12, 2011 05:33
K&R exercise 5-3, strcat
#include <stdio.h>
void *strcatsnot(char *s, const char *t);
void *strcatsnot(char *s, const char *t)
{
while (*++s)
;
while (*s++ = *t++)
;
@radiofreejohn
radiofreejohn / rita.c
Created March 11, 2011 07:50
K&R exercise 4-12, recursive itoa attempt
#include <stdio.h>
#include <limits.h>
// K&R 4-12
// char *rita seems OK too but I don't want to return a char to the original caller
// not sure if this is bad practice.
// will overflow if integer is longer than the target array (which shouldn't happen)
void *rita(char *target, long source)
{