need some help

This is a discussion on need some help within the C Programming forums, part of the General Programming Boards category; i have a question i want to find the size of the names i entered so i can loop that ...

  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,047
    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,047
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 5 of 5 FirstFirst 12345
Popular pages Recent additions subscribe to a feed

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