Struct question

This is a discussion on Struct question within the C Programming forums, part of the General Programming Boards category; i have a function which accepts a character as input parameter can i send in a data member of a ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Struct question

    i have a function which accepts a character as input parameter

    can i send in a data member of a struct which is of character type although it is inside the struct? is it valid to do this or would the function reject it coz it is a struct or accept it coz the data member is a character?
    Only by the cross are you saved...

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    struct test
    {
    char *ptr;
    };

    Is this what you mean? Sorry, very hard to understand your post.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    yeah
    something like that
    as long as the data member is of char type
    Only by the cross are you saved...

  4. #4
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I'm not exactly sure what you are asking. If you have a struct like this:
    Code:
    typedef struct Data{
    char Letter;
    int Num;
    } DATA;
    You can pass the entire stucture to a function:
    Code:
    int FunctionName(DATA D)
    {
         //you can access all the variables in the DATA struct
    return D.Num;
    }
    Or you can just pass the char part of the structure:
    Code:
    void FunctionName(char C)
    {
    //Do something with C
    }
    //Later on you would call:
    FunctionName(Data.Letter);
    This way you only pass the char part of the structure to the function. The function is then set up to only take a char. The other way you can pass the entire struct.

    Hope that helps.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    typedef struct Data{
    char Letter;
    int Num;
    } DATA;
    should be
    Code:
    typedef struct Data
    {
      char *Letters;
      int Num;
    } DATA;
    Is this what you want?

    Sorry about changing where the braces were. It's just my style preference to place the braces on a separate line - simply because I think its easier to read.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    actually wat i meant was er.......
    for example

    function(char str){}

    so if i call this function when i have a struct like

    struct node{
    char letter;
    }test;

    can i write it like this

    function(test.letter);
    Only by the cross are you saved...

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    didn't sean345 describe that above?

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    7 posts to get a simple yes. Maybe next time you should post a code sement to illustrate what you are asking to avoud confusion.

    Also, what does "er......." mean in so many of your posts? Is it short for something?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    er......., is like a way of me saying that i'm thinking of wat to type...
    Only by the cross are you saved...

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    okok, well, another question, if i have a struct like below :

    struct node{
    int lim;
    char test;
    };

    struct node testing;

    instead of assigning one by one testing->lim=0; and testing->test='0';

    can i just do testing={0,'0'};?
    Only by the cross are you saved...

  11. #11
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    but even

    struct node empty_test = { 0, '0' };
    ...
    testing = empty_test;

    struct node empty_test = { 0, '0' }; will still have to be dec lared at the start rite?

    in C we can't make a declaration halfway through, or can we?
    Only by the cross are you saved...

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    wat a shame...if onli there was a mechanism available where we could just loop through each data member of a struct...if not i have to specify the variable for every data member in the struct...
    Only by the cross are you saved...

  13. #13
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    oh yah, also is it true that if i declare a pointer of void type, that i can be cast to point to anoter variable of any type?

    for example, it can be made to point to a char or int?
    Only by the cross are you saved...

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    If you want to do it through a file, then yes, you can do it:

    struct node mynode;
    FILE *fp;

    fread ((char *)&mynode, 1, sizeof(mynode), fp);

    will read the entire contents of the structure from a file. fwrite will write the entire contents of the structure to a file.

    NB: Although the above will work in theory, I'm not sure I got the parameters the right way around (fp could be first, can't remember)

  15. #15
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    yeah, but wat if my structure contains both char and int el ements?
    Only by the cross are you saved...

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  2. Struct question... difference between passing to function...
    By Sparrowhawk in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2009, 02:59 PM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  5. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM

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