void stat(){
double a[500];
}
void vla(size_t n){
double a[n];
}
void mal(){
double* a=(double*)malloc(500*sizeof(double));
}
void maf(){
double* a=(double*)malloc(500*sizeof(double));
free(a);
}
double* mar(){
double* a=(double*)malloc(500*sizeof(double));
return a;
}
struct vector {double x,y,z;};
struct vector v = {1,2,3};
struct vector x = {1,2,3};
struct vector v = [1,2,3];
struct vector u = {.x=1,.y=2,.z=3};
struct vector w = {.z=3,.y=2,.x=1};
vector y;
typedef struct vector vector;
vector z;
ls *.c > cfiles.txt
ls *.c 1> cfiles.txt
ls *.c 2> cfiles.txt
ls *.c &> cfiles.txt
ls $PWD/*.c | awk -F/ '{print $NF}' > cfiles.txt
rm -f cfiles.txt; for i in *.c; do echo $i >> cfiles.txt; done
main function?
int main(int argc, char* argv[]) { /* do stuff */ }
main function into
double or int number?
#include<stdio.h>
#include<stdlib.h>
double* foo(){ static double a[]={0,0,0}; return a; }
int main(){
double* a=foo(); a[2]=1;
double* b=foo(); b[2]=2;
printf("a[2] = %g\n",a[2]);
return EXIT_SUCCESS;}
#include<stdio.h>
#include<stdlib.h>
double* foo(){ double* a=(double*)malloc(3*sizeof(double)); return a; }
int main(){
double* a=foo(); a[2]=1;
double* b=foo(); b[2]=2;
printf("a[2] = %g\n",a[2]);
free(a);a=NULL;
free(b);b=NULL;
return EXIT_SUCCESS;}
typedef struct {double re,im;} my_complex;
my_complex my_complex_add(my_complex a, my_complex b); /* returns a+b */
my_complex my_complex_sub(my_complex a, my_complex b); /* returns a-b */
my_complex my_complex_mul(my_complex a, my_complex b); /* returns a*b */
my_complex my_complex_div(my_complex a, my_complex b); /* returns a/b */
my_complex my_complex_set(double x, double y); /* returns x+i*y */
my_complex my_complex_conjugate(my_complex z); /* returns complex conjugate */
my_complex my_complex_exp(my_complex z); /* returns complex exponential function of the argument: you are only allowed to use real functions from math.h here ;) */
void my_complex_print(my_complex z); /* prints the complex number */
int my_complex_equal(my_complex a, my_complex b); /* returns 1 if equal, 0 otherwise */
Hint:
typedef struct {double re,im;} my_complex;
my_complex my_complex_add(my_complex a, my_complex b){
double x = a.re + b.re, y = a.im + b.im;
my_complex result = {.im = y, .re = x}; /* ;) */
return result;
}
int my_complex_equal(my_complex a, my_complex b){
if( a.re==b.re && a.im==b.im) return 1;
else return 0;
}
...
my_complex a = {1,2}, b = {3,4}, r = {4,6};
my_complex c = my_complex_add(a,b);
if( my_complex_equal(c,r) ) printf("test 'add' passed :) \n");
else printf("test 'add' failed :( \n");
typedef struct {int nrows,ncols; double* data;} my_matrix;.
my_matrix* my_matrix_alloc(int nrows, int ncols); /* allocates memory for a matrix nrows-times-ncols */
void my_matrix_free(my_matrix* m); /* frees the memory */
void my_matrix_set(my_matrix* m, int i, int j, double value); /* m_{ij} = value */;
double my_matrix_get(my_matrix* m, int i, int j); /* returns m_{ij} */
void my_matrix_set_identity(my_matrix* m); /* m = unity matrix */
void my_matrix_set_zero(my_matrix* m); /* all elements = 0 */
Implement range checking using <assert.h>.
typedef struct {int nrows,ncols; double* data;} my_matrix;
my_matrix* my_matrix_alloc(int nrows, int ncols){
my_matrix* m = (my_matrix*)malloc(sizeof(my_matrix));
if( m==NULL ){fprintf(stderr,"error in my_matrix_alloc\n");return NULL;}
(*m).nrows = nrows;
(*m).ncols = ncols;
(*m).data = (double*)malloc(nrows*ncols*sizeof(double));
return m;
}
void my_matrix_free(my_matrix* m){ free(m->data); free(m); }
void my_matrix_set(my_matrix* m, int i, int j, double value){
assert(0<=i && i<(*m).nrows);
assert(0<=j && j<(*m).ncols);
int indx = i*(*m).ncols+j;
(*m).data[indx]=value;
}