char array doesn't print the char in the array

This is a discussion on char array doesn't print the char in the array within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <conio.h> /* count lines in input */ main() { clrscr(); int c, nl; char a[30]; nl ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    8

    char array doesn't print the char in the array

    Code:
    #include <stdio.h>
    #include <conio.h>
    /* count lines in input */
    main()
    {
    clrscr();
    int c, nl;
    char a[30];
    nl = 0;
    scanf("%s",a[30]);
    printf("%c",a[1]);
    getch();
    return 0;
    }
    If I try to input "abc". It doesn't print the 'b'. Can someone help me in this one. If I have succeed in this code I can make the program that I needed. Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    This is wrong:
    Code:
    scanf("%s",a[30]);
    a is an array of 30 chars, therefore a[30] does not exist. Furthermore, even if a[30] did exist, you should be passing a pointer as the argument to scanf. What you probably intended:
    Code:
    scanf("%29s", a);
    Now, a is converted to a pointer to its first element. The 29 ensures that a maximum of 29 characters are read into a, with the last character reserved for the null character.

    By the way, <conio.h>, clrscr and getch and non-standard, and you don't really need them here. You should explicitly declare main as returning an int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    41
    Try "scanf("%s",&a);"
    This will work.

    a[30] is a char, not a string. One more thing you should keep in mind is when you are working with scanf you have to provide the address of variable not the variable.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,728
    Quote Originally Posted by sana.iitkgp
    Try "scanf("%s",&a);"
    This will work.
    That may work, but it is wrong because &a is a pointer to an array of 30 chars, but the %s format specifier expects a corresponding pointer to a char. Oh, and then the %s allows for a buffer overflow.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2012
    Location
    Quezon City, Philippines, Philippines
    Posts
    8
    Thanks guys. It is now working.
    Now I can continue on my work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. uincode char array to array<unsigned char,1>^
    By ripspinner in forum Windows Programming
    Replies: 5
    Last Post: 12-14-2009, 04:41 PM
  3. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  4. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 06:19 PM
  5. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 05:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21