Thread: sizeof / strlen which one?

  1. #1
    Registered User
    Join Date
    Mar 2008

    sizeof / strlen which one?


    I have the following:

    function (char *message) {
    printf("sizeof(message) is: %d", sizeof(message));
    printf("strlen(message) is: %d", strlen(message));

    char line_buf[300];
    fgets(line_buf, sizeof(line_buf), stdin); // message entered at stdin: "the cat sat"
    And I get the following at runtime:

    sizeof(message) is: 4
    strlen(message) is: 12

    Why is the sizeof(message) returning 4 and how do you correct this?


  2. #2
    Join Date
    Oct 2007
    Inside my computer
    You failed to specify a return type for your function. And further, you return nothing either, so it's undefined behavior. Add "void" to the return type.
    Anyhow, this is not something you can "fix." You are trying to get the size of a pointer, and on a 32-bit machine, the pointer is always going to be 4 bytes.
    Strlen inspects the string by iterating through it until it finds '\0' and returns how long it has traversed the string. In other words, it finds its length. Sizeof does not.

    You're not going to be able to find the length or size of a buffer from a pointer. It's not possible. The usual way around this is to pass in an extra argument with the size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    The larch
    Join Date
    May 2006
    sizeof returns the size of the char* which is always 4 (on your system), so it is correct.

    When you pass an array to a function, it degrades to a pointer and you cannot use sizeof anymore to determine the size of the original array.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Mar 2008
    Thanks guys. I'll just use strlen. It seems to work.

    I just made up the example in my post. I didn't bother to copy my actual program as it would take too long, so just ignore the lack of return and return type


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM
  4. Model not showing up on screen...
    By Shamino in forum Game Programming
    Replies: 14
    Last Post: 03-09-2006, 08:00 PM
  5. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM