#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[]); –
is it the copy of the array that is passed, or only a copy of the pointer
to the first element of the array?
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 vector_add(double v[], double u[]); /* v+=u */
void vector_subtract(double v[], double u[]); /* v-=u */
void vector_scale(double v[], double a); /* v=a*v */
void cross_product(double v1[], double v2[], double v[]); /* v = v1 x v2 */
double dot_product(double v1[], double v2[]); /* returns v1.v2 */
double vector_norm(double v[]); /* returns |v| */
void vector_print(double v[]); /* prints the components of the vector */
void vector_set(double v[], double v_1, double v_2, double v_3); /* v={v_1,v_2,v_3} */
void vector_set_zero(double v[]); /* v=0 */
void vector_set_basis(double v[], int k); /* v_{k}=1, v_{i!=k}=0 */
int vector_equal(double v[], double u[]); /* returns 1 if v==u, 0 otherwise */
Hint: something like
void vector_add(double v[], double u[]){
for(int i=0;i<3;i++) v[i]+=u[i];
return;}
double v[]={1,2,3}, u[]={3,2,1}, r[]={4,4,4};
vector_add(v,u);
if( vector_equal(v,r) ) printf("test 'add' passed :) \n");
else printf("test 'add' failed :( \n");