←to practical programming

Questions "pointers"

  1. How are arguments passed to functions in C: by-value or by-reference? And what does that mean?
    Hint: C syntax → argument passing.
  2. If double x=1.23, what is *(&x)?
  3. What is NULL? Hint: null pointer.
  4. What happens to variables declared inside a function when the function exits (returns)?
  5. What is a static variable? Hint: static variable.
  6. What will the following three programs print out and why?
    #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; }
    
  7. If you pass an array to a function with the signature void f(double a[]) – what is actually passed to the function:
    1. a copy of the whole array?
    2. the pointer a to the first element of the array?
    3. a copy of the pointer to the first element?
    4. something else?
    Hint: C syntax → argument passing → array parameters.
  8. When the function with the signature void f(double a[]) gets the array as parameter – can it figure out the size of the array?
  9. If you declare an array as int a[5]; and then try a[999]=1; what will might happen? Hint: Segmentation fault / causes.
  10. If you declare an i) static, ii) variable-length, iii) dynamic array inside a function, can the function return it?