2

Here is some simple code that prints struct values

in_hotel_info function is used to get struct inputs.(And yes, I use 'gets' because my professor forced me to use it sadly). And also When I put "0" as an input, it ends and returns its input numbers. And I used sscanf to scan strings and numbers.

#include <stdio.h>

typedef struct hotel_info hotel;
struct hotel_info
{
    char name[30];
    int rank;
    double popular;
    double far;
    char breakfast;
};
int in_hotel_info(struct hotel_info *p);
void out_hotel_info(struct hotel_info *p, int N, int G, double D);


int main(void)
{
    hotel hotels[100];
    hotel *p;
    int number = 0, ranks, fars, i;
    number = in_hotel_info(hotels);
    p = hotels;
    printf("%d\n", number);
    getchar();
    for (; p < p+number; p++)
    {
        printf("%s %d %lf %lf %c\n", p->name, p->rank, p->popular, p->far, p->breakfast);
    }
}

int in_hotel_info(struct hotel_info infos[])
{
    char inputs[100];
    hotel* p;
    p = infos;
    int cnt = 0;
    while (1)
    {
        gets(inputs);
        if (strcmp(inputs, "0") == 0)
        {
            break;
        }
        else
        {
            sscanf(inputs, "%s %d %lf %lf %c", p->name, &p->rank, &p->popular, &p->far, &p->breakfast);
        }
        p++;
        cnt++;
    }
    return cnt;
}

The problem is, when I tried to print

for (; p < p+number; p++)
    {
        printf("%s %d %lf %lf %c\n", p->name, p->rank, p->popular, p->far, p->breakfast);
    }

what I expected is

mike 2 3.5 4.24 Y

but I constantly got a segmentation error.

1
  • Read documentation of your C compiler (e.g. GCC..) and debugger (e.g. GDB). Read also the C11 draft standard n1570. Consider using the Clang static analyzer or Frama-C. With GCC, compile with gcc -Wall -Wextra -g Commented Oct 17, 2020 at 7:58

1 Answer 1

5

The problem is, when I tried to print

for (; p < p+number; p++)
{
    printf("%s %d %lf %lf %c\n", p->name, p->rank, p->popular, p->far, p->breakfast);
}

the problem is p < p+number is always true when number is strictly positive, so the for never ends and you access out of the array with an undefined behavior (your segmentation fault).


you have additional problems

  • gets is very dangerous to use because it can write out of the array, use fgets or scanf (secifying max length to read), in the considered case you can read a number then check if it is 0 or not

  • in in_hotel_info in case the user enter more than than entrie you write out of the array, you need to get the max number of element to read in argument

  • when you read p->name in case the enter name longer than 29 you write out of the array, limit the size using %29s rather than %s. ALso to bypass spaces at the beginning of the name use %29s (with a space before)

  • you do not check scanf returns 5, so you do not detect invalid inputs

The getchar(); in main is strange

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.