Thread: need some help

  1. #61
    Registered User
    Join Date
    Sep 2006
    Location
    vancouver wa
    Posts
    221
    i have a question i want to find the size of the names i entered so i can loop that amount to process that amount for there ages
    Code:
    struct family {             // family database
           char name[BUFSIZ];
           unsigned age;
           } tree[BUFSIZ];
    Code:
    size = sizeof name 
        printf("there size = %d", size);
    returns: 512
    Last edited by mrsirpoopsalot; 10-14-2006 at 10:02 AM.

  2. #62
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What were you expecting it to return? It returns the amount of memory taken up in memory by name. If you want to figure out how many indicies of name you've used, keep a separate counter.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #63
    The C eater *munch*
    Join Date
    Oct 2006
    Posts
    101
    use strlen instead

  4. #64
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yes, use strlen(). sizeof() an array will return how much memory the array takes up. (To figure out how many elements are in the array, divide that number by the size of one element: sizeof(array)/sizeof(array[0]).) strlen() will return the length of the string stored in the array.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #65
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    65 posts - is it done yet?
    Ask another question in a new thread if you still have specific issues.
    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.

Popular pages Recent additions subscribe to a feed