0
#include<stdio.h>
#include<string.h>

struct s {
    char ch[20];
    float a;
};

int main()
{
    struct s p[10];
    int i;
    for(i=0;i<10;i++)
    {
        scanf("%s%f",p[i].ch,p[i].a);
    }
}

What is wrong with this code?

Its giving runtime error.

What's the problem?

0

5 Answers 5

7

Most of the errors come from this line.

scanf("%s%f",p[i].ch,p[i].a);

You should use the address of p[i].a, and also restrict the numbers of chars to write in p[i].ch.

scanf( "%19s%f", p[i].ch, &p[i].a );
Sign up to request clarification or add additional context in comments.

5 Comments

Correct reasoning with the upper limit with %nums. +1. It is worth mentioning that even if you try to enter more characters, the %19s will simply ignore the additional ones.
You should also mention that the code should be if (scanf("%19s%f", p[i].ch, &p[i].a) != 2) { ...read error... }.
@JonathanLeffler From man scanf: "s" Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically.
What @JonathanLeffler meant was to add check for the number of inputs taken by scanf i.e. the return value which is 2 since it takes input in two fields total.
@JonathanLeffler @fayyazkl I was rather trying to interpret the help of scanf to check whether it is actually 19 or 20 to be used. I wrote 19 at first shot, then corrected to 20 after reading the excerpt of man scanf, then was corrected to 19 again ... I wrote a sample program. Definitely, it's 19.
3

I haven't touched C code for a while but shouldn't it be something like

scanf("%s%f",p[i].ch,&(p[i].a));

(You have to give the memory address of the variables to the scanf function.)

Comments

2

At the line:

scanf("%s%f", p[i].ch, p[i].a);

You are using p[i].a as a float* (pointer), while it's a float. You're invoking undefined behavior. You probably wanted to do it like this:

scanf("%s%f", p[i].ch, &p[i].a);

Comments

2

Change your code like this:

#include <stdio.h>
#include <string.h>
struct s {
    char ch[20];
    float a;
};

int main(){
    struct s p[10];
    int i;
    for(i=0;i<10;i++){
        scanf("%s%f",p[i].ch, &p[i].a);
    }
}

Note that variable a is a float type; you need to pass its memory address when using scanf.

Comments

2

I think the problem is in the p[i].a parameter; use &p[i].a instead.

2 Comments

@LiranElisha why alloc? It is already allocated fine. At least for me, that is why you didn't get the upvote
It WAS there in the answer above BEFORE the silent edit :) The fundamental flaw of not using address op was fine though. Also the other answer elaborated with %<num>s restricting the input length. Hence i believe it deserves the higher upvotes :).

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.