Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can't we store the element in the first element (index:0) of the character array?

When the program was debugged,
at i=0, it is not accepting any input. But from i=1 onwards it is accepting the input

and this is not happening in the case of normal array(array of type int)

What I have tried:

<pre lang="C++">
#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    char a[n];
    int i;
    for(i=0;i<n;i++)
    scanf("%c",&a[i]);
}
Posted
Updated 1-May-21 2:49am
Comments
Richard MacCutchan 1-May-21 8:28am    
That code will not even compile so you cannot debug it. The line char a[n]; is not valid in C, as already mentioned in a previous question.

Well, it appears that gcc allows it.
OriginalGriff 1-May-21 8:46am    
Are you sure about that? It works fine for me with the GDB compiler:
https://www.onlinegdb.com/online_c_compiler
Richard MacCutchan 1-May-21 8:58am    
Microsoft's cl compiler doesn't accept it:
test.cpp(18): error C2131: expression did not evaluate to a constant
OriginalGriff 1-May-21 10:04am    
That's because it's C90 compliant, not C99.

Gawd, how do I remember this kind of trivia? :D
Richard MacCutchan 1-May-21 10:17am    
"how do I remember this kind of trivia?"
I'd like to know the answer to that. I struggle to remember most things these days.

Is does work: it just probably doesn't do what you expect it to.
When you use scanf for a number, it waits until the user enters the number and presses ENTER, then parses the input and returns the numeric value - but the ENTER character is "left on the shelf" so when you subsequently use scanf for the individual characters, the first one returned is the ENTER character: '\n'
Then the remaining characters are read, so it looks like you end up one short...
Try this and see if it helps:
C
scanf("%d\n", &n);
 
Share this answer
 
Comments
Richard MacCutchan 1-May-21 8:50am    
Snap. But I really, really hate scanf.
OriginalGriff 1-May-21 8:56am    
I know what you mean, but ... for it's day, and all that! :laugh:
Adding the \n specifier to the format strings appears to resolve it:
C++
int n;
scanf("%d\n",&n); // tell scanf to consume the newline
char a[n];
int i;
for(i=0;i<n;i++)
{
    scanf("%c\n",&a[i]);
    printf("a[%d]: %c\n", i, a[i]); // print the stored character
}


[edit]
I should have mentioned that this was using gcc under Linux. Microsoft's C/C++ compiler does not accept dynamic stack allocations.
[/edit]
 
Share this answer
 
v2
Comments
k5054 1-May-21 10:21am    
Did you try this? For me, this just hangs in scanf.
Richard MacCutchan 1-May-21 10:30am    
Yes, but using gcc under Linux. But see also my comment to OG above.
k5054 1-May-21 11:11am    
Interesting. I've hit enter about 10 times after entering a number, and gdb says I'm still in scanf. eg:
[k5054@localhost]$ cat example.c
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    printf("enter a number: ");
    scanf("%d\n", &n);
    printf("n = %d\n", n);

    return 0;
}
[k5054@localhost]$ gcc example.c -o example
[k5054@localhost]$ ./example.c
bash: ./example.c: Permission denied
[k5054@localhost]$ ./example
enter a number: 10





^C
[k5054@localhost]$ gdb example
GNU gdb (GDB) Fedora 10.1-4.fc33
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from example...
(No debugging symbols found in example)
(gdb) r
Starting program: /home/ebacon/tmp/example 
enter a number: 10



^C
Program received signal SIGINT, Interrupt.
0x00007ffff7ec4442 in __GI___libc_read (fd=0, buf=0x4056b0, nbytes=1024) at ../sysdeps/unix/sysv/linux/read.c:26
26        return SYSCALL_CANCEL (read, fd, buf, nbytes);
(gdb) bt
#0  0x00007ffff7ec4442 in __GI___libc_read (fd=0, buf=0x4056b0, nbytes=1024) at ../sysdeps/unix/sysv/linux/read.c:26
#1  0x00007ffff7e55122 in _IO_new_file_underflow (fp=0x7ffff7f96800 <_IO_2_1_stdin_>) at libioP.h:948
#2  0x00007ffff7e563a6 in __GI__IO_default_uflow (fp=0x7ffff7f96800 <_IO_2_1_stdin_>) at libioP.h:948
#3  0x00007ffff7e2ed5b in __vfscanf_internal (s=<optimized out>, format=<optimized out>, argptr=argptr@entry=0x7fffffffd930, 
    mode_flags=mode_flags@entry=2) at vfscanf-internal.c:3023
#4  0x00007ffff7e2c712 in __isoc99_scanf (format=<optimized out>) at isoc99_scanf.c:30
#5  0x0000000000401163 in main ()
(gdb) quit
A debugging session is active.

        Inferior 1 [process 195134] will be killed.

Quit anyway? (y or n) y
[k5054@localhost]$ 
[k5054@localhost]$ lsb_release -ir
Distributor ID: Fedora
Release:        33
[k5054@localhost]$ 
Richard MacCutchan 1-May-21 11:21am    
Same here. No matter how often I try I never get scanf to do what it is supposed to do. Your code sample (and mine incidentally) is still waiting for another input value.
_-_-_-me 1-May-21 11:38am    
Why it is waiting for other input value?
Thank you !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900