-2
#include<stdio.h>
int main()
{
int N,i,j,n,*numArray,count=0;
char **arr,*part1,*part2,*token;
scanf("%d",&N);
arr=(char **)malloc(N*sizeof(char *));
numArray=(int *)malloc(N*sizeof(int));
for(i=0;i<N;i++){
arr[i]=(char *)malloc(50*sizeof(char));
}

for(i=0;i<N;i++){
printf("plz enter %d th :",i);
    gets(&arr[i][0]);// why is it not executing

}

for(i=0;i<N;i++){
 printf("%s",arr[i]);
}
return 0;
}

I tried executing this code and found that the line gets(&arr[i][0]); does not get executed, i.e. it doesn't wait for user to input. Instead, it prints "plz enter 0 th: plz enter 1 th: plz enter 2 th: and so on" and doesn't wait for user to enter the string. I am unable to get what exactly is wrong and what exactly is happening? Plese help. Thanks in advance.

gj1103
  • 127
  • 2
  • 10
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Jun 08 '16 at 19:05
  • `newline` left in buffer. – Weather Vane Jun 08 '16 at 19:18
  • And what value does `scanf()` return? You need to check that. – Andrew Henle Jun 08 '16 at 19:19
  • @AndrewHenle scanf() in this program let user inter number of sentences – gj1103 Jun 08 '16 at 19:21
  • We understand that. The question is what is the return value of the `scanf` function? If it returns 0, it didn't read anything. Also, what is your input when you get this behavior? – dbush Jun 08 '16 at 19:23
  • @dbush when i enter 3 (i.e. N=3), the output that i get is: plz enter 0 th: plz enter 1 th: plz enter 2 th:, this means that scanf is reading value for N successfully and is not returning 0 – gj1103 Jun 08 '16 at 19:24
  • @SouravGhosh thanks for the downvote :) – gj1103 Jun 08 '16 at 19:30
  • 1
    @gj1103 Just because I left a comment, I become the downvoter? :)) – Sourav Ghosh Jun 08 '16 at 19:32
  • I venture to suggest putting `[c] gets skips line` in a search box will yield some insight into the problem (this question has *many* duplicates, but most are so arbitrarily titled it's ridiculous). – WhozCraig Jun 08 '16 at 19:39
  • Possible duplicate of [Input using gets not working, but working with scanf](http://stackoverflow.com/questions/16300121/input-using-gets-not-working-but-working-with-scanf) –  Jun 08 '16 at 20:41
  • 2
    `gets` is not standard C (anymore). **Never ever** use it! Use `fgets` on `stdin` instead. And check the values provides from external input before using them and functions for errors which are relevant for the program execution. Never rely on correct input data! – too honest for this site Jun 09 '16 at 02:59
  • And if you need a 2D array, use one! Your code does not have anything which can relresent a 2D array. – too honest for this site Jun 09 '16 at 03:00
  • for ease of readability and understanding of code, by us humans, 1) separate code blocks (for, if, else, while, do...while, switch, case, default) via a blank line. 2) consistently indent the code. indent after every opening brace '{'. un-indent before every closing brace '}'. suggest using 4 spaces (never tabs) for each indent level. 3) follow the axiom: *only one statement per line and (at most) one variable declaration per statement. 4) use meaningful variable names. A variable name should indicate usage or content (or better, both). – user3629249 Jun 09 '16 at 14:55
  • the expression `sizeof(char)` is defined in the standard as 1. multiplying anything by 1 has no effect. So all that expression, in the call to `malloc()` is doing is cluttering the code, making it more difficult to understand, debug, maintain. – user3629249 Jun 09 '16 at 15:08
  • when compiling, always enable all the warnings, then fix those warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -pedantic` I also use: `-Wconversion -std=gnu99` ) The posted code will cause the compiler to output a long string of warning messages (12 warnings with `gcc) that need to be fixed. – user3629249 Jun 09 '16 at 15:12
  • amongst other things, the posted code is missing the statement: `#include ` so the `malloc()` function is properly defined. – user3629249 Jun 09 '16 at 15:13

2 Answers2

3

This line inputing the number of entries

scanf("%d",&N);

leaves a newline in the input buffer. Then this line

gets(&arr[i][0]);

takes that lone newline as the first entry.

You can get rid of it like this

scanf("%d%*c",&N);

But you should not be using gets in this day and age, it is obsolete. This would have been better for the string entries (instead of the above mod)

scanf("%50s", arr[i]);

as well as checking the return value from all the scanf calls. The code still needs improvemnt though, since as stated scanf will only scan up to the first whitespace.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • its unbelievable! has been too long since i started coding in c and its the first time I face this situation. Its tough to digest for me that why I never faced this before. Also please suggest what to use in place of that? scanf("%[^\n]*c",&arr[i][0]) – gj1103 Jun 08 '16 at 19:40
  • I hate `scanf`. I would rather use `fgets` and remove the trailing `newline`. But the main point is not to mix your input methods. AFAIK you would use `scanf("%50[^\n]",arr[i])` but that's a guess as it's not a syntax I use. – Weather Vane Jun 08 '16 at 19:42
  • but the fact that i met this situation dissappointed me. I am unable to digest. Thanks @weather – gj1103 Jun 08 '16 at 20:13
  • this line: `scanf("%d",&N);` leaves white space in stdin. Suggest using: `scanf("%d ",&N);`. Notice the trailing space in the format string. – user3629249 Jun 09 '16 at 15:03
  • @user3629249 unfortunately that only works (MSVC) if you want the next input at the same time. `scanf("%d ",&N);` does not return until the following input is also given. – Weather Vane Jun 09 '16 at 15:23
  • @WeatherVane Could you please help me by answering http://stackoverflow.com/questions/37764258/why-character-pointer-is-getting-assigned-just-a-character-while-a-string-is-su?noredirect=1#comment62997129_37764258 – gj1103 Jun 11 '16 at 14:02
2

. it doesn't wait for user to input. Instead, it prints "plz enter 0 th: plz enter 1 th: plz enter 2 th: and so on"

this is due to problem of white space in your loop... instead try consuming them before every time you scan string using scanf(" ");, like this :

for(i=0;i<N;i++){
printf("plz enter %d th :",i);
    scanf(" "); //to consume white spaces
    gets(arr[i]);// why is it not executing? because of wrong arguments

}

EDIT : as suggested by @user3629249

Never use gets() for two major reasons:

  1. It allows the input to overflow the input buffer
  2. It is removed from the language from C11 onward.

a better alternative would be fgets()

and here's a link to know about it more: here

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37