1 #include 2 #include 3 #include s struct _string_pair t f 6 char * str1; char *
ID: 3723109 • Letter: 1
Question
1 #include 2 #include 3 #include s struct _string_pair t f 6 char * str1; char * str2; 9 typedef struct _string pair t string pair t; 10 u int main(void) f 12 char* line= NULL size t sz; 13 14 string pair t ** array; 15 int count=0; 16 while (getline (&line; , &sz;, stdin) >= 0) { 17 18 19 20 21 array = realloc (array, (count+1) * sizeof (*array)); array [count] = malloc (sizeof (array [count])); array [count]->str1 - strdup(line); char * p = strchr (array [count]->str 1, ,-) ; if (p != NULL) { array [count]->str2 = strdup(strchr (line , -') + 1); 23 else 25 array [count]->str2 NULL; = 26 28 counttt; 29 30 for (int i-0; i str2); printf ("%s %s ", array [1]->str1, free(array[i]->stri); free(array[i]->str2); 31 34 as free (array); 36return EXIT SUCCESS;Explanation / Answer
1.Error 1
(a)Line numer: 16
(b)Problem: Identifier "getline" is undefined
(c) Fix:
add below lines after the header files of #incude <string.h>
#include<istream>
std::istream& getline(char* s, size_t n, char delim);
replace line 16 with
//reads string in a line until new line charater is encountered
while (getline(line, sz,' '))
becauze getline never returns integer value
2.Error 2
(a) Line number: 17
(b)problem: Error: a value of type "Void*" cannot assigned to an entity ype "_string_pair_t**"
(c) fix:
replace line 17 with below code
array =(_string_pair_t**)(realloc(array, (count + 1)*sizeof(array)));
bcz realloc function syntax was wrong
3.Error 3
(a) Error : line 18
(b)problem: Error: a value of type "Void*" cannot assigned to an entity ype "_string_pair_t**"
(c) Fix:
replace line 18 with below code
array = (_string_pair_t**) malloc(sizeof(array[count]));
because assignement and malloc definition is wrongly used
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<istream>
std::istream& getline(char* s, size_t n, char delim);
struct _string_pair_t
{
char *str1;
char *str2;
};
typedef struct _string_pair_t string_pair_t;
int main(void)
{
char *line = NULL;
size_t sz;
string_pair_t ** array;
int count = 0;
while (getline(line, sz,' '))
{
array =(string_pair_t**)(realloc(array, (count + 1)*sizeof(array)));
array = (string_pair_t**) malloc(sizeof(array[count]));
array[count]->str1 = strdup(line);
char *p = strchr(array[count]->str1, '=');
if (p != NULL)
{
*p = '';
array[count]->str2 = strdup(strchr(line,'=') + 1);
}
else
{
array[count]->str2 = NULL;
}
count++;
}
for (int i = 0; i < count; i++)
{
printf("%s = %s ", array[i]->str1, array[i]->str2);
free(array[i]->str1);
free(array[i]->str2);
}
free(array);
return EXIT_SUCCESS;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.