#include<stdio.h>
void f(int i){i=0;}
int main(){
int i=1; f(i); printf("i=%i\n",i);
return 0; }
#include<stdio.h>
void f(int* i){*i=0;}
int main(){
int i=1; f(&i); printf("i=%i\n",i);
return 0; }
#include<stdio.h>
void f(int* i){i=NULL;}
int main(){
int i=1; f(&i); printf("i=%i\n",i);
return 0; }
void do_something_with_an_array(int a[]); –
what is actually passed to the function (the copy of the array? the pointer to the first element? or what?).
sizeof with arrays.
int a[] = {0,1,2,3,4};
int b[5];
int n = (double)rand()/RAND_MAX*10+1;
int c[n];
int *d=(int*)malloc(n*sizeof(int));
do_something_with_an_array subroutine, will it be possible
to determine the sizes of the arrays inside the subroutine?
int a[5]; and then try
a[7]=1; what will happen? Hint: Segmentation
fault / causes.
#include<stdio.h>
int i=2; /* file scope */
void f(){printf("i=%i\n",i);}
int main(){
int i=1; /* function scope */
{
int i=0; /* block scope */
printf("i=%i\n",i);
}
printf("i=%i\n",i);
f();
return 0; }
double arrays. The signatures should be like
void tri_vector_add(double v[], double u[]); /* v+=u */
void tri_vector_subtract(double v[], double u[]); /* v-=u */
void tri_vector_scale(double v[], double a); /* v=a*v */
void tri_vector_cross_product(double v1[], double v2[], double v[]); /* v = v1 x v2 */
double tri_vector_dot_product(double v1[], double v2[]); /* returns v1.v2 */
double tri_vector_norm(double v[]); /* returns |v| */
void tri_vector_print(double v[]); /* prints the components of the vector */
void tri_vector_set(double v[], double v_1, double v_2, double v_3); /* v={v_1,v_2,v_3} */
void tri_vector_set_zero(double v[]); /* v=0 */
void tri_vector_set_basis(double v[], int k); /* v_{k}=1, v_{i!=k}=0 */
int tri_vector_equal(double v[], double u[]); /* returns 1 if v==u, 0 otherwise */
Hint: something like
void tri_vector_add(double v[], double u[]){ for(int i=0;i<3;i++) v[i]+=u[i]; }
double v[]={1,2,3}, u[]={3,2,1}, r[]={4,4,4};
tri_vector_add(v,u);
if( tri_vector_equal(v,r) ) printf("test 'add' passed :) \n");
else printf("test 'add' failed :( \n");