- How are arguments passed to functions in C:
by-value or by-reference? And what does that mean?
Hint: C syntax → argument
passing.
- If
double x=1.23
, what is *(&x)
?
- What is
NULL
? Hint: null pointer.
- What happens to variables declared inside a function when the function exits (returns)?
- What is a static variable? Hint: static variable.
- 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; }
- If you pass an array to a function with the signature
void f(double a[])
–
what is actually passed to the function:
- the copy of the array?
- the pointer to the first element?
- the copy of the pointer to the first element?
- something else?
Hint: C syntax → argument passing → array
parameters.
- When the function with the signature
void f(double a[])
gets the array as parameter – can it figure out the size of the array?
- At which stage—compilation or execution of the
program—is the memory
for the following arrays allocated? And which of these arrays
"know" their sizes in the scope where they are declared?
int a[] = {0,1,2,3,4};
int b[5];
int n = (argc>1 ? atoi(argv[1]):5); /* get n from the argument to the program */
int c[n];
int *d=(int*)malloc(5*sizeof(int));
- If you declare an array as
int a[5];
and then try
a[7]=1;
what will happen? Hint: Segmentation
fault / causes.
- If you declare an i) static, ii) variable-length, iii) dynamic array
inside a function, can the function return it?
- What will the following C-program print?
#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; }