0

scanf(ch_arr[0], "type");

The above snippet used in a program:

char ch_arr[3][10] = {
"spike",
"tom",
"jerry"
};

This is invalid:

ch_arr[0] = "tyke"; // invalid

ch_arr[1] = "dragon"; // invalid

While this is valid

scanf(ch_arr[0], "type");

Can someone explain why this is valid because I expect scanf to take in a type specifier as its first argument

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • `C` and `C++` are different languages. – Jason Aug 27 '22 at 11:12
  • 2
    `scanf(ch_arr[0], "type");` *isn't* valid. It is trying to use a string literal `"type"` as the input target, but it is write-only. As for the format spec, you can pass any string you like and `scanf` will try to input according to whatever rules it contains. – Weather Vane Aug 27 '22 at 11:13
  • Refer to [array to pointer decay](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay) – Jason Aug 27 '22 at 11:13
  • `ch_arr[0] = "tyke";` is invalid because that is not how to copy a string in C. Use `strcpy()`. – Weather Vane Aug 27 '22 at 11:17
  • @WeatherVane: `scanf(ch_arr[0], "type");` is valid because `ch_arr[0]` does not contain any directives that cause assignments, so `scanf` never tries to write into `"type"` or even use it at all, and C 2018 7.21.6.2 says “If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.” – Eric Postpischil Aug 27 '22 at 11:35
  • @EricPostpischil in this fumbling situation, but in normal use the argument would be expected to be available to be used, but it can't be. Just saying. Tell me, why pass an argument that can't be used? – Weather Vane Aug 27 '22 at 11:40
  • @WeatherVane: A reason to pass an argument that cannot be used is sometimes the format string is computed, and it is easier to write one `scanf` call with a fixed list of arguments some of which cannot be used with some of the computed formats than to dispatch to different `scanf` calls. But that is not relevant; the issue is not whether there is an use in passing an unused argument. The issue is that telling a student that something is invalid when it is not may confuse them about what the rules are… – Eric Postpischil Aug 27 '22 at 12:04
  • … You may know there is no reason to use a string literal as an argument after the first, but that is not what you wrote. You wrote that it is not valid. Then, if the reader assumes that is true, they have to figure out some reason it is not valid. But there is no rule that it is not valid, so, unless they figure out what you wrote is wrong, which we should not expect a student to be able to do, they can only come up with some incorrect rule. And then they will have a mistake in their head, and it will be harder to teach them correct rules later. – Eric Postpischil Aug 27 '22 at 12:05
  • @EricPostpischil passing that argument is never correct. Saying that it is ok because it is never used may have confused the student even more. Teaching the student to pass arguments that are incorrect, and then to say that is ok because they are never used, is not the way forward. Keeping it simple is. – Weather Vane Aug 27 '22 at 12:35
  • "I expect scanf to take in a type specifier as its first argument". The first argument to `scanf` is a format string which may contain conversion specifiers. It may contain any number of conversion specifiers, including zero. – William Pursell Aug 27 '22 at 13:00

0 Answers0