CrazyEngineers
  • linked list in C

    durga ch

    Member

    Updated: Oct 26, 2024
    Views: 1.0K
    OK!
    I declare few nodes . lets say my node is named as sortElement.

    now I want to store the memory locations of these nodes in an array

    How do i declare that?
    I tired something this way but I encountered an error in linux

    struct sortElement
    {
    declaration;

    };
    sortElement *element[24];

    what i am trying to do here is declare an array element which can store the locations of all my nodes
    will this work?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Corpse-Thrust

    MemberJun 2, 2009

    I guess C doesn't support creating pointers to custom data types. Only C++ does.

    Try creating a pointer of type void and using it to store the memory locations of the nodes.
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberJun 2, 2009

    Corpse-Thrust
    I guess C doesn't support creating pointers to custom data types. Only C++ does.
    C language do support creation of pointers to user defined data type. Below is one of the CE posts where i have defined pointer to user defined data type in C:

    #-Link-Snipped-#


    durga
    I declare few nodes . lets say my node is named as sortElement.

    now I want to store the memory locations of these nodes in an array

    How do i declare that?
    I tired something this way but I encountered an error in linux

    struct sortElement
    {
    declaration;

    };
    sortElement *element[24];

    what i am trying to do here is declare an array element which can store the locations of all my nodes
    will this work?
    The above code will not work due to two reasons:
    1. The declaration inside the body of struct should be a valid variable declaration (e.g., "int declaration;").
    2. You are trying to create an array of pointers to sortElement, but sortElement is not a valid data type. The valid type is "struct sortElement" or you need to define a data type using typedef.

    The declaration should be as:
    struct sortElement {
      int declaration;  //or something else as required
    };
    struct sortElement *element[24];
    
    or
    typedef struct {
      int declaration;  //or something else as required
    } sortElement_t;
    sortElement_t *element[24];
    
    Let us know if you face issue with above change.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • durga ch

    MemberJun 2, 2009

    Hi CT, Pradeep,

    Thanks for the reply.
    Firstly 'declaration' is not my variable. I have list of declarations and I did do typedef

    The actual code is as below:



    typedef struct sortElement sortElement;
    struct sortElement
    {
    int elementnumber;
    int topflag;
    int bottonflag;
    sortElement *zero;
    sortElement *one;
    struct inputbuf1;
    struct inputbuf2;
    struct outputbuf1;
    struct outputbuf2;

    };
    sortElement *element[24];
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberJun 2, 2009

    As you are declaring correct variables inside the body of struct so there is only one issue is the declaratio
    sortElement *element[24];
    is not correct. The declaration should be as:
    struct sortElement {
      //variable declaration
    };
    struct sortElement *element[24];
    
    or
    typedef struct {
       //variable declaration
    } sortElement_t;
    sortElement_t *element[24];
    
    Let us know if you face issue with above change.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • durga ch

    MemberJun 2, 2009

    thanks! But what is wrong with "typedef struct sortElement sortElement;"

    is it not same as saying take "struct sortElement" as sortElement?



    P.S: I have few more syntax related questions to ask, is it OK if I continue int his thread?

    Thansk again!
    Are you sure? This action cannot be undone.
    Cancel
  • pradeep_agrawal

    MemberJun 2, 2009

    durga
    thanks! But what is wrong with "typedef struct sortElement sortElement;"

    is it not same as saying take "struct sortElement" as sortElement
    When we declare a struct as
    struct structName {
      //declaration of variables
    };
    
    The declaration of variable of the above type will look like:
    struct structName xyz;

    As per C syntax you can not declare a variable as "structNamexyz ;" because structName is currently not a data type "struct structName" is a data type.

    'typedef' can be used to define a data type to avoid using 'struct' with 'structName' while you are declaring variables.

    The typedef will look like:
    typedef struct structName {
      //variable declaration
    } structName_t;
    
    Here i have defined a data type structName_t of type "struct structName". Now you can use "structName_t" instead of "struct structName".

    The declaration of variable using the newly defined data type can be done as:
    structName_t xyz;

    Let me know if any item need more clarification.

    P.S.: For query on different topic start new thread.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • durga ch

    MemberJun 2, 2009

    got it! thanks for that
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register