Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Identify syntax, runtime, and logic errors t. Structs and Arrays (4 points) Belo

ID: 3855923 • Letter: I

Question


Identify syntax, runtime, and logic errors

t. Structs and Arrays (4 points) Below is a short codo segment which is supposed to read in lines of text from the user, read until ). It then stores these strings in the array of a str Arry. stops reading when the user pointer to the st,Arroy tvpes "stop". It then returns a pointer to the str Array top It then returns Unfortunately, there are a number of syntax, runtime, and logic errors in this code. Identity at least 1 errors and briefly describe each error : struct strArr ( int aize chararay 6 typedef struct strårr strArray S strArray* readArray ( ){ 9struct strarray ar 10 char (256] temp 11 arr size0: 12 arr array (char)malloc( sizeof (char) 13 :4 while scant (n] ", temp) ) 15 16 if temp "stop") 17 18 break 19 arr.size+ 20 21 arr array (char*) realloc( arr size sizeof (char); (char**) realloc ( 1)) arr , array arr, size * sizeof (char) ); *(arr, array (arr , size + temp ; - = 23 24 return karr

Explanation / Answer

1) struct strArray arr;

This line contains a syntax error . You should have strArr instead of strArray as strArr is the name of the structure.

2) char [256] temp;

This contains a syntax error too. Instead of using the square brackets before the array object variable, we have to use it after.

char temp[256];

3) if (temp=="stop").

This contains a logical error. Strings in C cannot be compared using the equality operator. Use of strcmp() is appreciated.

4)arr.array=(char **)realloc(arr.size*sizeof(char));

This contains a runtime error. invalid conversion from 'unsigned int' to 'void*' [-fpermissive]|. This type of conversion in c is not allowed.

5) arr.array=(char **)realloc(arr.size*sizeof(char));

This aslo contains a Syntax Error . The definition of realloc() function says that it should have two arguments.Prototype definition of realloc().

void *realloc(void *ptr, size_t size);

So it also needs the argument that states what entity is being reallocated.

void *realloc(void *ptr, size_t size);

So it also needs the argument that states what entity is being reallocated.