In: Computer Science
Correct mistakes in the following program:
/* BUG ZONE!!!
Example: some common pointer errors */
#include <stdio.h>
main()
{
int i = 57;
float ztran4;
int track[] = {1, 2, 3, 4, 5, 6}, stick[2][2];
int *nsave;
/* Let's try using *nsave as an int variable, and set it to 38 */
*nsave = 38; /* BUG */
nsave = NULL;
*nsave = 38; /* BUG */
nsave = 38; /* BUG */
&nsave = 38; /* BUG */
nsave = &i;
*nsave = 38;
nsave = &ztran4; /* BUG */
nsave = track; /* nsave points at track[0] */
/* Increase track[0] by 1: */
*nsave++; /* BUG */
/* Now point at stick: */
nsave = stick; /* BUG */
nsave = &stick; /* BUG */
nsave = stick[0][0]; /* BUG */
nsave = *stick;
nsave = **stick; /* BUG */
nsave = stick[0];
nsave = &stick[0][0];
nsave = &**stick;
}
#include <stdio.h>
main()
{
int i = 57;
float ztran4;
int track[] = {1, 2, 3, 4, 5, 6}, stick[2][2];
int *nsave;
int q = 38; /*For assigning 38 to variable otherwise it would corrupt other data structures in the program and get errors instead*/
/* Let's try using *nsave as an int variable, and set it to 38 */
nsave = &q; /* BUG */
nsave = NULL;
nsave = &q /* BUG */
nsave = &q; /* BUG */
nsave = &q; /* BUG */
nsave = &i;
*nsave = 38;
void * v= &nsave; /*For assigning different data type pointer*/
v = &ztran4; /* BUG */
nsave = track; /* nsave points at track[0] */
/* Increase track[0] by 1: */
nsave++; /* BUG */
/* Now point at stick: */
int (*nsave)[2] = stick; /* BUG */
int (*nsave)[2] = stick; /* BUG */
int (*nsave)[2] = stick; /* BUG */
nsave = *stick;
nsave = *stick; /* BUG */
nsave = stick[0];
nsave = &stick[0][0];
nsave = &**stick;
}
Hope this helps you. Please give an upvote. Thank you.