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)?

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.