Thread: char array doesn't print the char in the array

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05:41 PM
  3. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  4. signed char array to unsign char array.
    By beon in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 07: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, 06:18 PM