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

C Programming Hello, i have this code and when i try to run ir it appears in mi

ID: 3765128 • Letter: C

Question

C Programming

Hello, i have this code and when i try to run ir it appears in mi screen stream!=Null in line 29. I dont see what is wrong in line 29, can someone maybe help me thank you!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct {
   int x, y, z, w;
   char os[8];
} host;

host convertIP(char s[]){
   int a, b, c, d;
   sscanf(s, "%d.%d.%d.%d", &a, &b, &c, &d);
   host temp;
   temp.x = a;
   temp.y = b;
   temp.z = c;
   temp.w = d;
   return temp;
}

void printHost(host *h){
   printf("%d.%d.%d.%d %s ", h->x, h->y, h->z, h->w, h->os);
}
void findHosts(host *net, int size, host t, int len){
   int i;
   for(i = 0; i < size; i++){
       if(len >= 24){
           if(abs(net[i].w - t.w) < pow(2, 32 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
       }
       else if(len >= 16){
           if(abs(net[i].z - t.z) < pow(2, 24 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
       }
       else if(len >= 8){
           if(abs(net[i].y - t.y) < pow(2, 16 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
       }
       else if(len >= 0){
           if(abs(net[i].x - t.x) < pow(2, 8 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
       }
   }
}

int main(){
   FILE *fptr;
   host *arr;
   fptr = fopen("hosts.txt", "r");
   char ip[16];
   char o[8];
   int num = 0;
   arr = (host *) malloc(sizeof(host));
   while(fscanf(fptr, "%s %s", ip, o) != EOF){
       arr = realloc(arr, (num + 1) * sizeof(host));
       arr[num] = convertIP(ip);
       strcpy(arr[num].os, o);
       num++;
   }
   printf("Enter the target IP address: ");
   scanf("%s", ip);
   printf("Enter the os, or enter "?" for any os: ");
   scanf("%s", o);
   host h = convertIP(ip);
   strcpy(h.os, o);
   int len;
   printf("Enter the length of the network part: ");
   scanf("%d", &len);
   findHosts(arr, num, h, len);
}

Explanation / Answer

Use this

while(fscanf(fptr, "%s %s", &ip, &o) != EOF){
       arr = (host* )realloc(arr, (num + 1) * sizeof(host));
       arr[num] = convertIP(ip);
       strcpy(arr[num].os, o);
       num++;
   }

problem was at fscanf(fptr, "%s %s", &ip, &o) you have to use &ip instead of ip and &o instead of o

and use (host*) in realloc to explicitly convert type, dont depend in implicit conversion. Sometimes it doesn't happen and gives error.

here is the full code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

typedef struct {
   int x, y, z, w;
   char os[8];
} host;

host convertIP(char s[]){
   int a, b, c, d;
   sscanf(s, "%d.%d.%d.%d", &a, &b, &c, &d);
   host temp;
   temp.x = a;
   temp.y = b;
   temp.z = c;
   temp.w = d;
   return temp;
}

void printHost(host *h){
   printf("%d.%d.%d.%d %s ", h->x, h->y, h->z, h->w, h->os);
}
void findHosts(host *net, int size, host t, int len){
   int i;
   for(i = 0; i < size; i++){
       if(len >= 24){
           if(abs(net[i].w - t.w) < pow(2, 32 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
       }
       else if(len >= 16){
           if(abs(net[i].z - t.z) < pow(2, 24 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
       }
       else if(len >= 8){
           if(abs(net[i].y - t.y) < pow(2, 16 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
            }
       else if(len >= 0){
           if(abs(net[i].x - t.x) < pow(2, 8 - len)){
               if(strcmp(t.os, "?") == 0 || strcmp(t.os, net[i].os) == 0) printHost(&net[i]);
           }
       }
   }
}

int main(){
   FILE *fptr;
   host *arr;
   fptr = fopen("hosts.txt", "r");
   char ip[16];
   char o[8];
   int num = 0;
   arr = (host *) malloc(sizeof(host));
   while(fscanf(fptr, "%s %s", &ip, &o) != EOF){
       arr = (host* )realloc(arr, (num + 1) * sizeof(host));
       arr[num] = convertIP(ip);
       strcpy(arr[num].os, o);
       num++;
   }
   printf("Enter the target IP address: ");
   scanf("%s", ip);
   printf("Enter the os, or enter "?" for any os: ");
   scanf("%s", o);
   host h = convertIP(ip);
   strcpy(h.os, o);
   int len;
   printf("Enter the length of the network part: ");
   scanf("%d", &len);
   findHosts(arr, num, h, len);
}