Thread: help with struct

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    help with struct

    hello guys,
    i have been thinking of my college project work given to me. where in one sutitation i was suppose to check where a structure is empty are not. for instance
    Code:
    struct something
    {
         int something1;
         int something2[20];
    };
    
    
    int main()
    {
           struct something some[10];
    
          if(how do i check that some[1] struct variable is empty)
    }
    is there any way that i can check that.

    i got one more thing to be clarified about strtok fucntion, lets take an example
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char str[]= {"This is a test string"};
        char *p;
        
        p=strtok(str," ");
        while(p != NULL)
        {
            printf("Token : %s\n",p);
            p=strtok(NULL," "); 
        }
        printf("The original string \" %s \"",str);
        
        getchar();
        return 0;
    }
    /*my ouput
    Token : This
    Token : is
    Token : a
    Token : test
    Token : string
    The original string " This "
    */
    if u look at that code the first call to function strtok is with the orginal string 'str' and with the second call i am sending the update pointer NULL as a parameter. and at the end i wanted to print the original string of 'str'. but after using strtok my original string is changed which just contains the first word. is there any way that i can get back that string. well i thought of one way that's copy the original string to some other string before the sending the orginal string to strtok.

    well, it might me simple thing which might have confused me a lot, can any one explain this please

    ssharish2005
    Last edited by ssharish2005; 12-12-2005 at 07:40 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    if(how do i check that some[1] struct variable is empty)
    Check all members for zero (or whatever it was initialized to), and strings for "".
    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. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    When you declare a struct as an automatic variable (inside a function), it's filled with undefined values unless you initialise it. There's no such concept as "empty". Initialise the struct to some sentry values (perhaps 0), and use that to denote "empty".

    As for strtok, it will always put nulls into your string. Make a copy if you want it untouched.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    p=strtok(str," ");
    That's not how you use strtok(). At least, I don't think it is.

    [edit]
    Yes it is; sorry. http://faq.cprogramming.com/cgi-bin/...&id=1044780608
    [/edit]
    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. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, the best way that i can keep original string safe is just have a copy of it. as crw says.

    ssharish2005

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    To keep the original string unmodified, consider using strcspn or sscanf instead of using strtok.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    thax very my DAVE is got sorted

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM