Test these codes, explain your answer.
1. consider the following program
int main(int argc, char* argv[]){
FILE* fp = fopen(argv[1],"r");
return 0;
}
Compile the code: gcc main.c
Run the code: ./a.out
Will throw a segmentation fault.
Use debugger to figure out where the errors are
2. Write this code to a program and see what happens.
int* ptr = malloc(4);
free(ptr);
scanf("%d",*ptr);
use the GDB to find the error
3. The following code is supposed to swap two strings (char*'s)
What can go wrong with the code? Assume A[i] and A[j] are strings with pre-allocated memory to hold strings s1 and s2.
char tmp[50];
strcpy(tmp,A[i]);
strcpy(A[i],A[j]);
strcpy(A[j], tmp);
4. What are some of the differences between char A[50] and char* A = malloc(50)?