Thread: struct question

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    86

    struct question

    Code:
    typedef struct
    {
    int a =1;
    }
    test_struct;
    
    
    test_struct *test_obj = NULL;
    
    int main ()
    {
    
    }
    how do i change *test_obj to a value and not null in main i am confused

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    "test_obj" is a pointer that is not pointing to anything.

    If you want to use the pointer to create a new instance of the struct, you need to allocate memory for it.

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    Code:
    typedef struct{
    int a;
    }test_struct;
    
    int main(){
    test_struct.a = *value;
    return 0;
    is this what do you need?

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Quote Originally Posted by Giwrgows x View Post
    Code:
    typedef struct{
    int a;
    }test_struct;
    
    int main(){
    test_struct.a = *value;
    return 0;
    is this what do you need?
    no its not i need test_obj to equal to something but not null

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    If you want to point to a structure that already exists, just assign its address to the pointer:

    Code:
    test_struct test;
    test_struct *ptr = &test;
    If you want to create a new instance of the struct, you need to allocate memory for it:

    Code:
    test_struct *test = malloc(sizeof(*test));  // be sure to free your memory when you're done with it

  6. #6
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    Code:
    typedef struct{
    int a;
    }test_struct, *test_pnt;
     
     
    test_struct mystruct = {1};
    
     
    int main ()
    {
     test_pnt myptr = &mystruct;    
     myptr->a = 2;     // change it to two            
    }
    Typedef allow the shortcut of assigning pointer declaration

  7. #7
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Typedef allow the shortcut of assigning pointer declaration[/QUOTE]

    Typing test_struct mystruct has the same number of keystrokes as struct test mystruct

    Also, please don't hide a pointer using typedef for absolutely no reason at all; it's confusing!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  2. Struct delcaring a struct within itself question
    By illidari in forum C Programming
    Replies: 3
    Last Post: 12-03-2010, 03:01 PM
  3. Question about struct
    By mikeman in forum C Programming
    Replies: 39
    Last Post: 12-07-2008, 07:43 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. Question about list (struct {..struct *next}
    By jmzl666 in forum C Programming
    Replies: 1
    Last Post: 10-23-2002, 01:34 AM

Tags for this Thread