Skip to content

Instantly share code, notes, and snippets.

Created September 18, 2015 15:58
Show Gist options
  • Save anonymous/a5f2ffeb003206401f80 to your computer and use it in GitHub Desktop.
Save anonymous/a5f2ffeb003206401f80 to your computer and use it in GitHub Desktop.
auto-posted gist
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char **argv) {
//argc is the number of command line arguments, *INCLUDING* the name of the executable file itself
//argv[0] is the executable file name
//argv[1] is the first actual provided command line argument
if(argc == 1) {
fprintf(stderr, "Usage: %s n\n", argv[0]);
exit(1);
}
int i;
for(i=0; i<argc; i++) {
printf("argv[%d] = %s\n", i, argv[i]);
}
return 0;
}

Functions: Passing By Value vs Passing by Reference

  • Motivating Demonstration: swap.c

Pointers in C

int a = 10;
double b = 10.5;


//To declare a pointer variable that can point to an int variable:
int *ptrA;
//int * indicates it an int pointer, ptrA is the variable's name

//Declare a pointer variable that can point to a double variable:
double *ptrB;

//at this point, ptrA and ptrB point to: who knows

//WRONG: make ptrA point to 10:
ptrA = 10;
//Problems: there may not be a memory address "10"
//  even if there is it probably does not belong to us
//  likely outcome: segmentation fault

//Special memory location called NULL
ptrA = NULL;
ptrB = NULL;
//initializing pointers to NULL is good practice

//later on you can check for NULL:
if(ptrA == NULL) {
  printf("ERROR: bad memory location\n");
}

//Want: make ptrA point to the variable a
//need a way to get the memory location of the variable a
// The referencing operator gives this to us:
ptrA = &a;
//ampersand, when applied to a normal variable, gives us its memory location

//We also want the opposite: that is, given a pointer
// variable, we want a way to access what is stored at
// that memory location 
// need: the DEreferncing operator, simply just the asterisk
*ptrA = 15;

//POinters allow you to define functions that can take
// pointer variables as parameters.
// you are passing the memory location of the variables, rather than
// their values


//PITFALLS: 

//1. You *can* dereference a normal variable
int x = 10;
*x = 20;
//assigns the value 20 to the memory address 10
//likely results in a seg fault

//2. You *can* reference a pointer variable
int *p = NULL;
int x = &p;
//&p gives the address of the pointer (the address of the address) and places it into the variable x

//3. You can really screw up and do
int x = 10;
*(*(*(x))) = 20;
&(&(&(x))) = 15;

//Summary:
// The referencing operator is the ampersand (&)
//  it takes a NORMAL VARIABLE and turns it into a pointer value
// The dereferencing operator is the asterisk (*)
//  it takes a POINTER VARIABLE and turns it into a normal variable
#include<math.h>
#include "quadratic.h"
int getRoots(double a, double b, double c, double *root1, double *root2) {
//what kind of errors could face here?
//segmentation fault: if root1 or root2 were NULL, then we would have a problem
//if a == 0 we only have a single root, division by zero
//discriminant is complex then we have NaN
//or... NO ERRORS!
if(root1 == NULL || root2 == NULL) {
return NULL_POINTER_ERROR;
} else if(a == 0) {
return DIVISION_BY_ZERO_ERROR;
} else if(b*b - 4*a*c < 0) {
return COMPLEX_NUMBER_ERROR;
}
*root1 = (-b + discriminant(a, b, c)) / (2*a);
*root2 = (-b - discriminant(a, b, c)) / (2*a);
return NO_ERROR;
}
double discriminant(double a, double b, double c) {
//principle: we should NOT handle errors in our functions, instead
//if an error occurs, we should COMMUNICATE the error to the calling
// function, then IT can decide what to do
return sqrt(b*b - 4*a*c);
}
double root01(double a, double b, double c) {
return (-b + discriminant(a, b, c)) / (2*a);
}
double root02(double a, double b, double c) {
return (-b - discriminant(a, b, c)) / (2*a);
}
/**
* Computes the discriminant of a quadratic equation
*/
double discriminant(double a, double b, double c);
/**
* This is a single funciton with two pass-by-reference variables
* that computes both roots of a quadratic equation
*/
void getRoots(double a, double b, double c, double *root1, double *root2);
/**
* This function computes the "positive" root of a quadratic
* polynomial ax^2 + bx + c
*
*/
double root01(double a, double b, double c);
/**
* This function computes the "negative" root of a quadratic * polynomial ax^2 + bx + c
*
*/
double root02(double a, double b, double c);
#include<stdlib.h>
#include<stdio.h>
#include "quadratic.h"
int main(int argc, char **argv) {
if(argc != 4) {
fprintf(stderr, "Usage: %s a b c\n", argv[0]);
exit(1);
}
double a = atof(argv[1]);
double b = atof(argv[2]);
double c = atof(argv[3]);
//double r1 = root01(a, b, c);
//double r2 = root02(a, b, c);
double r1;
double r2;
int errorCode = getRoots(a, b, c, &r1, &r2);
if(errorCode == 1) {
printf("Null pointer error!\n");
} else if(errorCode == 2) {
printf("Division by zero\n");
} else if(errorCode == 3) {
printf("complex number error!\n");
}
printf("The roots are %f and %f\n", r1, r2);
return 0;
}
#include<stdlib.h>
#include<stdio.h>
void swap(int a, int b);
void swapByRef(int *a, int *b);
int main(int argc, char **argv) {
int x = 10;
int y = 20;
printf("x, y = %d, %d\n", x, y);
swap(x, y);
printf("x, y = %d, %d\n", x, y);
swapByRef(&x, &y);
printf("x, y = %d, %d\n", x, y);
printf("x is a variable stored at memory location %p and it contains the value %d\n", &x, x);
return 0;
}
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
return;
}
void swapByRef(int *a, int *b) {
//create a normal temporary variable temp
//dereference the pointer a so that you can place its value into the temp variable
int temp = *a;
//deference a and b, making them into normal variables to swap the values
*a = *b;
*b = temp;
return;
}
public class SwapDemo {
private static int a;
private static int b;
public static void swap(Integer a, Integer b) {
Integer temp = a;
b = a;
a = temp;
}
public static void main(String args[]) {
Integer x = 10;
Integer y = 20;
System.out.println("x = " + x + ", y = " + y);
SwapDemo.swap(x, y);
System.out.println("x = "+ x + ", y = " + y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment