Strings

This is a discussion on Strings within the C Programming forums, part of the General Programming Boards category; I am trying to store the date in memory in form of dd/mm/yy for an assignment. And although it does ...

  1. #1
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8

    Strings

    I am trying to store the date in memory in form of dd/mm/yy for an assignment. And although it does what I want, it compiles with errors. Which is a problem because one of the requirements is no errors when strict ANSI/ISO compliance is on.
    I am getting the error, warning:format'%11s'expects type 'char*', but argument 2 has type 'char (*)[11]'
    I have already tried changing the char type and no luck

    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    
    {
    
    char date[11];
    
    printf("What is the date?\n");
    scanf("%11s",&date);
    
    printf("%s",date);
    return 0;
    
    }
    Help me please

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    is there a reason you're using %11s instead of just %s?

  3. #3
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8
    Yes, but I didnt count that characters properly, it is supposed to be 10. I actually want it in the form dd/mm/yyyy and if there are more than 10 characters added it will cut out the extra ones, so it doesnt mess up my form.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Since you said it's an assignment I really hate to spoon feed you the answer. Compare your scanf and printf and how you pass the char array. Should be obvious.

    HINT: Remember a char is an array so if you don't have an index listed it is REFERENCING the address to the first element.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Hi,

    Which compiler you are using?
    It compiles fine in 'Visual Studio'
    with no errors

    Regards,
    Siddu

  6. #6
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8
    Quote Originally Posted by mayday View Post
    Remember a char is an array so if you don't have an index listed it is REFERENCING the address to the first element.
    I don't really understand what you mean. I am just new to this and the date thing is a tiny part of my program and is not used in any of the calculations. As for my compiler, I am using Quincy 2005.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    &date is pointer to array of 11 chars

    date is array - which means when passing to function it is converted automatically to pointer to the fist element - in your case it is pointer to char, exactly what scanf expects

    %11s is too match - you need the space for the nul-character, so correct format will be
    Code:
    scanf("%10s", date);
    the problem here - you do not know if there is something left in the input buffer because user entered something wrong... in which case you need to ask about input again...

    so pobably combination of fgets (with long buffer) and sscanf will be more stable and error prone




    Quote Originally Posted by Siddu_Kyocera View Post
    Which compiler you are using?
    It compiles fine in 'Visual Studio'
    with no errors
    It does not mean that the code is correct - just that VS cannot catch such type of errors
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User okel91's Avatar
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    8
    It is compiling now without any errors. Thanks so much vart

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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