Thread: Compiler warning question

  1. #1
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

    Question Compiler warning question

    After reading Salem's post about using -Wall with the gcc compiler I decided to give it a try. I'm getting a warning that has me a little confused.

    warning is
    adt.c:104: warning: char format, different type arg (arg 2)
    adt.c:106: warning: char format, different type arg (arg 2)
    The code for those two lines are:
    Code:
    printf("  Last Name:  ");
    scanf("%s", &tln);
    printf("  First Name: ");
    scanf("%s", &tfn);
    The two variables are:
    Code:
    char tln[name_len], tfn[name_len];
    and name_len is defined by:
    Code:
    #define name_len        20
    Anyone want to venture a guess on what this means? Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > scanf("%s", &tln);
    Should be
    scanf("%s", tln);

    For an array, ltn and &ltn have the same value, but different types (which is why it works).


    If on the other hand you had pointers

    char *ltn = malloc( 20 );
    scanf("%s", &tln);
    That would be very wrong (both the type and the value would be wrong).

    char *ltn = malloc( 20 );
    scanf("%s", tln);
    is correct (as far as the types are concerned)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I think you are saying that for arrays you do not need to provide the address when using scanf? What about when you want to pass by reference to another routine?

    I did remove the & from the calls and that did get rid of the warning. I understand what you are saying about the values and types. However I thought that using & passed the address of the variable and not it's value.

    Man I'm glad I'm taking a class next semester. I see my self taught C have left a lot of holes.

    Thanks Salem.

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    For an array, ltn and &ltn have the same value.

    Now this is something new, I thought &ltn == &&ltn[0], it turned out that &ltn == &ltn[0]
    weird, why does work so? Isn't ltn supposed to be a pointer to the first element of the array?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When you pass an array to a function, you get (as you know) a pointer to the first element of the array

    So arr and &arr[0] are the same thing (by value and by type).

    A less frequent (and occasionally damn useful) feature is a pointer to the whole array, which you get by saying &arr
    This has the same value (like what you would see if you were to print each of the following using %p with printf), but the type is completely different. And yes, the () are necessary - char *r[10] is a completely different animal to char (*r)[10]

    char arr[10];
    char *p = arr;
    char *q = &arr[0];
    char (*r)[10] = &arr;

    Now r is useful because you can also allocate a char matrix[x][10] in one easy step using
    r = malloc ( x * sizeof *r );

    If you look carefully, you'll see that sizeof *p and sizeof *q are 1, but sizeof *r is 10.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thanks Salem, I understand that now.

  7. #7
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Mmm, thanks Salem for the array pointer thing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler warning and a confusing function declaration.
    By PaulBlay in forum C Programming
    Replies: 3
    Last Post: 05-20-2009, 10:18 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Quick Warning Question
    By Paul22000 in forum C Programming
    Replies: 1
    Last Post: 05-02-2008, 05:20 PM
  4. Dev-cpp - compiler options
    By tretton in forum C Programming
    Replies: 7
    Last Post: 01-06-2006, 06:20 PM
  5. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM