Thread: sizeof

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    127

    sizeof

    What is the purpose of sizeof? When I delete the sizeof it shows the error about IntelliSense: argument of type "char *" is incompatible with parameter of type "int" but the program still can run.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    char buf[20];
    char *a;
    
    printf ("Please enter a line of text, max %d characters\n", buf);
    fgets(buf, buf, stdin);
    a = strchr(buf, '\n');
    *a= '\0';
    printf ("Thank you, you entered >%s<\n", buf);
    
    system("pause");
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    sizeof is a compile-time operator that computes the size, in characters, of its argument. It works on any non-void type (and a pointer to void is a non-void type), as well as on anything with a type (for example, variables). sizeof(char), which is equivalent to sizeof char, is defined to be 1.

    In your code, sizeof(buf) will be 20, since buf is an array of 20 char. sizeof(a) is an implementation defined value (since the size of a pointer is compiler dependent). sizeof(*a) will be 1 (as a is a pointer to char, so *a is a char).

    A common use of sizeof() is when dynamically allocating memory. malloc(n*sizeof(X)), if it succeeds, allocates enough memory to hold n X's.

    Since sizeof() yields a value of type size_t (an unsigned integral type) it should not be printed using printf()'s %s format. That is probably the reason for the error. Technically, IIRC, there is no direct way of printing a size_t in C other than converting it to unsigned long, printing that using %lu, and praying.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    You definitly need a sizeof here.
    Code:
    fgets(buf, sizeof(buf), stdin);
    The size of will find the size of buf, which in your case is 10 bytes. So that the fgets will only read 10 bytes of the user avoiding buffer overflow.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    Is this good practice to put sizeof in this line of code or is redundant?
    Code:
    printf ("Please enter a line of text, max %d characters\n", sizeof(buf));

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Is it good practice to walk to an apple tree or is it redundant?

    The answer to this question, like the answer to yours, depends on what you are trying to achieve. If you want to pick an apple, walking to an apple tree is good practice. If you don't want to pick an apple, walking to an apple tree is unnecessary (and possibly redundant).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Jun 2012
    Posts
    127
    So, the code
    Code:
    printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
    in FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com is redundant.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The point is, if you want to tell the user the maximum amount of characters to enter, the line is necessary and therefore -- by definition -- not redundant. If you don't want to tell the user this information, then yes, it's unnecessary and therefore redundant.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    And printing that information, redundant aspect aside, doesn't buy you out of verifying unknown input.

    Soma

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by ulti-killer View Post
    So, the code
    Code:
    printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
    in FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com is redundant.
    No, absolutely not.
    Redundant would mean that the effect is the same with or without it. If you had run the program with and without it then you would notice that with and without clearly produces different results. So no, of course it's not redundant.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 12-09-2010, 02:33 PM
  2. Replies: 6
    Last Post: 10-15-2007, 08:05 AM
  3. sizeof(cin) and sizeof(cout)
    By noobcpp in forum C++ Programming
    Replies: 11
    Last Post: 06-30-2007, 11:00 AM
  4. sizeof
    By agarwaga in forum C Programming
    Replies: 9
    Last Post: 10-18-2005, 08:34 AM
  5. sizeof~
    By black in forum C++ Programming
    Replies: 6
    Last Post: 08-05-2002, 07:03 PM