typedef struct: naming structures using pointers

This is a discussion on typedef struct: naming structures using pointers within the C Programming forums, part of the General Programming Boards category; My official 1st post; coming from x86 AsmLanguage now learning C for Linux sake. Linux rules! Refer to CProgrammingDotComForwardSlashTutorialFwdslashLess on7DotHTML ...

  1. #1
    Registered User BinaryBlock's Avatar
    Join Date
    Feb 2012
    Location
    Michigan
    Posts
    6

    typedef struct: naming structures using pointers

    My official 1st post; coming from x86 AsmLanguage now learning C for Linux sake. Linux rules!

    Refer to CProgrammingDotComForwardSlashTutorialFwdslashLess on7DotHTML
    respectively.

    Problem:
    Code:
    typedef struct { 
    int name_of_variable;
    }Tag;
    then:
    Code:
    char* structureName = "name_of_single_structure"; /*code-generated name*/
    Tag *structureName.name_of_variable = 33;
    Results: FAIL; Illegal usage perhaps?

    Question:
    How to make this work? I want my code to make it's own structure names, in this case, "name_of_single_structure", which would result to:
    Code:
     Tag name_of_single_structure; /*code-generated name*/
    name_of_single_structure.name_of_variable = 33;
    Is this possible?

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Why do you want to programatically generate names? No one but the programmer will see them.

    If you're trying to keep organized (with good names), just use an array. The last code snippet you posted looks right.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by BinaryBlock View Post
    Problem:
    Code:
    typedef struct { 
    int name_of_variable;
    }Tag;
    then:
    Code:
    char* structureName = "name_of_single_structure"; /*code-generated name*/
    Tag *structureName.name_of_variable = 33;
    Results: FAIL; Illegal usage perhaps?
    The first problem is that a pointer is nothing by a reference to a memory address. Above, you have no struct, you just have a pointer to a struct, which means you can not initialize it.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    What's with all the "dot" and "fwdslash" stuff? If you mean www.cprogramming.com/tutorial/lesson7.html then just say it.

    "Tag" is a very bad structure name. The term "tag" is usually used (in conjunction with other characters) after the word struct, not as the name of the typedef. So take this as an example:
    Code:
    typedef struct aStruct_tag {
        int anInt;
    } aStruct;
    With the above names, the code you gave that doesn't work is:
    Code:
    aStruct *structureName.anInt = 33;
    This doesn't work because you're defining a pointer, not a struct. Instead, try
    Code:
    aStruct myStruct;
    myStruct.anInt = 33;
    In what sense do you want the names to be generated programatically? And for what purpose?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User BinaryBlock's Avatar
    Join Date
    Feb 2012
    Location
    Michigan
    Posts
    6
    Thanks guys. Here's the reason for the "named" structures:

    If I want to, at will fetch the variables from each uniquely named structures via scanf() this will make it possible.
    This would be the only way (so far) to label them and will be a timesaver for me otherwise, there will be multiple entries
    under various names from within this Tag.

    Sorry. Most forums don't allow urls thus the mess. Just like what Alex Allain did I used snippets of his example thus, the Tag 'thingy'.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by BinaryBlock View Post
    If I want to, at will fetch the variables from each uniquely named structures via scanf() this will make it possible.
    I have no idea what you mean by this and why you couldn't use "struct Tag" instead. typedefs can be confusing since they hide the type.

  7. #7
    Registered User BinaryBlock's Avatar
    Join Date
    Feb 2012
    Location
    Michigan
    Posts
    6
    Quote Originally Posted by memcpy View Post
    Why do you want to programatically generate names? No one but the programmer will see them.

    If you're trying to keep organized (with good names), just use an array. The last code snippet you posted looks right.
    Hmm, you just may be right memcpy. If thats the case I'll have to sizeof the datafile, then create a strucure array and finally retrieve each array somehow...

    It sounds like I'll have to use malloc() in a fashion to array my struct, then have access to it all at will via my code commandline to manipulate data. Is it possible to have your code generate structures when necessary?
    -or-
    Set aside some malloc() and pump data in that way to retrieve it by the structure Tag and Name once its all ready?
    The file's purpose is only to supply raw data (unuseable) to the code, then my code will translate it to readable text that can be searched by my custom commandline (eventually, it'll be GNOME GUI: crawl before you walk).

  8. #8
    Registered User BinaryBlock's Avatar
    Join Date
    Feb 2012
    Location
    Michigan
    Posts
    6
    I guess what I'm looking for is a way to "feed" my structures some data under the same Tag with a different name. These names will include variables (stats) and all I have to do is "call" that Tag Name, and I'll get the .variables that are inside it.

    Ex:
    Me> check 200F (pressed enter)*
    program> found 3 listings under 200 degrees; thermo #34, thermo #17 and thermo #18
    Me> check any 250F to 500F (pressed enter)*
    program> nothing found thus far. Systems are normal!

    hope this clears it up a little Subsonics.
    Last edited by BinaryBlock; 02-05-2012 at 02:58 PM.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Well, you can't do like that anyway. "struct Tag" or "Tag" would be the type just like int or float, and only be present in your source code.

  10. #10
    Registered User BinaryBlock's Avatar
    Join Date
    Feb 2012
    Location
    Michigan
    Posts
    6
    Okay, I have a solution: The file I'll be reading from will be malloc'd into RAM, where the data can be manipulated.
    Then, using <string.h> I can summon a search in RAM for data and save results to fopen( ,"w") for later.

    This method I won't need a structure because my code will translate data, then save my reports
    to readable text using custom commands. That should suffice!

    I just thought I could allow my code to struct}; names for me but it can't name them and allow me
    to search by name after it's done doing so.

    Well, back to my pseudo-code guys...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. differences between struct and typedef struct
    By stanlvw in forum C Programming
    Replies: 1
    Last Post: 07-22-2008, 04:28 PM
  2. Diff btw typedef struct and struct?
    By willkoh in forum C Programming
    Replies: 3
    Last Post: 09-24-2005, 03:27 PM
  3. Need help naming data structures
    By AP2 in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2002, 10:36 AM
  4. typedef structures
    By Pointman in forum C Programming
    Replies: 4
    Last Post: 12-13-2001, 07:39 PM
  5. Naming pointer to function in a struct...
    By QuestionC in forum C Programming
    Replies: 4
    Last Post: 12-09-2001, 12:20 AM

Tags for this Thread


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