-2

Abnormal behavior of scanf() function.

#include<bits/stdc++.h>
using namespace std;

char c[100][100];
int main()
{
    for(int i=1;i<=10;++i) scanf("%s",c[i]+1);
}

my question is , since the we declare c as a 2 dimensional array , how can scanf take only one dimensional input , without any compilation error?

https://ideone.com/954oeO

is there some default value for the second parameter?

melpomene
  • 79,257
  • 6
  • 70
  • 127
  • [Don't `#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h/31816096#31816096). – melpomene Jul 04 '18 at 05:02
  • I don't understand what you're asking. What compilation error did you expect? – melpomene Jul 04 '18 at 05:03
  • shouldn't scanf() take the argument in the form c[i][j] , like scanf("%s",c[i][j]) as we declared c as a 2dimensional char array – nitin jain Jul 04 '18 at 05:06
  • That would also compile, but it would be broken because `%s` expects a `char *`, not a `char`. It would probably end up crashing your program. – melpomene Jul 04 '18 at 05:07
  • not actually the program didn't crashed ... – nitin jain Jul 04 '18 at 05:08
  • `c` is an array of 100 elements, each of which is `char[100]` - an array of 100 chars. `c[i]` refers to `i`th such array. An array name decays to a pointer to its first element, so `c[i]` can also be thought of as `&c[i][0]` - a pointer to the `0`th char of `i`th array. `c[i]+1` then points to the `1`st char of `i`th array. In other words, `c[i] + 1 == &c[i][1]`. – Igor Tandetnik Jul 04 '18 at 05:08
  • @nitinjain I said "probably". It's not guaranteed to crash (as always, broken C++ code can do anything it wants). – melpomene Jul 04 '18 at 05:09
  • Igor Tandetnik , yes i ran the program and it gives the same ouput in both cases . – nitin jain Jul 04 '18 at 05:12
  • @melprone , the more clear explanation is given by igor above ... in both cases the ouput is same .. i think 2 dimensional arrays are not just like we think in the form of tables but its actually in the form of arrays inside arrays ... Thanks Igor Tandetnik for the explanation . – nitin jain Jul 04 '18 at 05:14
  • 1
    First, I'm not sure what you mean by "both cases"; I only see one piece of code. Second, I never claimed that this code wold fail to run. Third, the piece of code I see doesn't contain any statements that might produce output, so I don't see how it can produce any output, whether same or different. – Igor Tandetnik Jul 04 '18 at 05:14
  • @nitinjain C++ has no 2-dimensional arrays, only arrays of arrays. – melpomene Jul 04 '18 at 05:16
  • Thanks Igor Tandetnik and melpomene for the help , now i understood it ... – nitin jain Jul 04 '18 at 05:32

1 Answers1

1

c[i] is the same as &c[i][0] due to array -> pointer conversion.
c[i] + 1 is the same as &c[i][1].

For that reason, use of

scanf("%s", c[i]+1);

is valid from a syntactic point of view.

However, keep in mind that if the input is too large to fit at the given location, you will run into the problem of accessing the array beyond the valid bounds. It will be better to use

scanf("%98s", c[i]+1);

There are 99 characters at c[i]+1. You need to have space for the terminating null character. Hence, the use of %98s.

R Sahu
  • 196,807
  • 13
  • 136
  • 247