sizeof question

This is a discussion on sizeof question within the C Programming forums, part of the General Programming Boards category; I was noticing that if you use sizeof to get the size of a char array that is not from ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    119

    sizeof question

    I was noticing that if you use sizeof to get the size of a char array that is not from to the function you are calling it from, it doesn't work. Even if you have a function that takes in a string, like function(char *string); and you refer to it as "string" instead of it's actual declaration name it still doesn't. Am I doing something wrong, and if not, anyone know why this is so?

    Code:
    void ash(char *string);
    
    main() {
    
    char str[50];
    
    //sizeof would work fine here
    
    return 0;
    }
    
    void ash(char *string) {
    
    printf("%d", sizeof(string)); <--why doesn't this work?
    
    printf("%d", sizeof(str)); // <--I understand why this doesn't   work, can't access a local var outside it's function
    
    } //

    btw, the freeware version of my program is done! as soon as i get a webpage to store it on, i'll post it here for all to d/l, cause i certainly couldn't have done it w/o everyone's help

  2. #2
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    You're getting the size of the pointer, not the size of the array.

    The size of a pointer is 32 bits on a 32 bit OS which translates to 4 bytes.

    The function is working perfectly, it's just doing something you didn't expect it to do

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    In this situation you should create your function so that the size of the array is specified.

    eg.
    Code:
    char str[10];
    fgets(str,sizeof(str),stdin);
    Other functions are fread, fwrite and lots of others that do this.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    63
    Quote Originally Posted by Ash1981
    btw, the freeware version of my program is done! as soon as i get a webpage to store it on, i'll post it here for all to d/l, cause i certainly couldn't have done it w/o everyone's help
    You can attach source code files to posts

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,453
    Read this -> http://c-faq.com/aryptr/index.html
    Focus on the parts which tell you about how arrays decay to pointers when you pass an array to a function (for example).

    sizeof(array) is one of the exceptional cases in which an array does not decay to a pointer, but since there is a function call in the way in your example, then it's already too late.

  6. #6
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    thanks for the replies, i'm still modifying the source code a bit, then i'll post it

    btw, I had a couple of other small questions that have been bugging me:

    Is there a way to convert a string into an integer or vice versa? I googled this, but I kept getting visual basic hits, lol.

    I know in the main function I always use exit(0) so the OS knows all went well, but if there is an error, do you use exit(1) or exit(-1), or if not those, what do you use, and what does this actually do?

    I know how to stop Ctrl-C from stopping my program, but how about ctrl-break?

    Thanks again
    Last edited by Ash1981; 02-01-2006 at 01:38 AM. Reason: mistake

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    119
    Oh one more thing, if anybody happens to read this, you know how you can usually right click on a program and select properties under winx, and it will allow you to change the icon of the program, and offer you a list of icons to choose from? well get this, that's not an option on mine, so I have to stick with the regular windows console icon...

  8. #8
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Quote Originally Posted by Ash1981
    thanks for the replies, i'm still modifying the source code a bit, then i'll post it

    btw, I had a couple of other small questions that have been bugging me:

    Is there a way to convert a string into an integer or vice versa? I googled this, but I kept getting visual basic hits, lol.

    I know in the main function I always use exit(0) so the OS knows all went well, but if there is an error, do you use exit(1) or exit(-1), or if not those, what do you use, and what does this actually do?

    I know how to stop Ctrl-C from stopping my program, but how about ctrl-break?

    Thanks again
    1. well, there are few lib fucntion to do that job that is to convert from string to int and visa versa. u can always look through atoi fucntion to get string to integer and itoa from int to string. as itoa fucntion is not a stand fucntion its better to use sprintf to covert from string to int

    2. its better to say return 0 or return 1 rather than exit(0) or exit(1). its just record for the OS. like for example if some file is not opened properly we normally say return 1 if you are in the main. returns are used only in the main

    3. clt-C is commanly used in the linux platforms and clt-break on wndows plat still clt-c works fine. i dont think there is any diff in clt-break and clt-C

    ssharish2005

  9. #9
    old man
    Join Date
    Dec 2005
    Posts
    90
    Quote Originally Posted by Ash1981
    I know in the main function I always use exit(0) so the OS knows all went well, but if there is an error, do you use exit(1) or exit(-1), or if not those, what do you use, and what does this actually do?
    If you include stdlib.h you can use "exit (EXIT_SUCCESS)" and "exit (EXIT_FAILURE)" ... (on my system these are defined as 0 and 1 respectively).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read from 15-pin port
    By C_ntua in forum C Programming
    Replies: 23
    Last Post: 07-11-2008, 09:09 AM
  2. sizeof union question
    By noops in forum C Programming
    Replies: 13
    Last Post: 06-06-2008, 11:56 AM
  3. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  4. Malloc,calloc..Sscanf.
    By ozumsafa in forum C Programming
    Replies: 22
    Last Post: 07-26-2007, 01:09 AM
  5. sizeof() EZ question
    By V1P3R V3N0M in forum Windows Programming
    Replies: 15
    Last Post: 01-11-2003, 10:25 PM

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