Thread: double pointer to a structure

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    17

    double pointer to a structure

    hi,
    I need to know how to deference a double pointer to a structure.

    i have a structure like this.
    insert
    Code:
    struct string1
    {
       unsigned char line1[20];
    
       unsigned char line2[30];
    
    
    }
    Now there is a function that returns the pointer to this structure.

    insert
    Code:
    struct string1 *linecount()
    {
        struct string1 string_var;
        //I fill the structure elements and return the pointer
        return &string_var
      
    
    }
    now in my main function i pass this structure pointer to another function and try to get the values of the structure members.But it gives junk values.

    insert
    Code:
    int main()
    {
       struct string1 *string_var2;
       char temp;
       string_var2 = linecount();
       temp=string_var2->line1[0]; //Dereferncing works fine here.
       func2(string_var2)
    
    
    }
    
    void func2(struct string1 *num)
    {
    
       //when i defernece the pointer iam getting junk values.
      char mychar;
       mychar=num->line1[0];
    
    }
    Please guide me why it happens and the way to solve it.

    Regards,
    Rohit

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    struct string1 *linecount()
    {
        struct string1 string_var;
        //I fill the structure elements and return the pointer
        return &string_var;
      
    
    }
    You are returning a pointer to a local variable. I am surprised your compiler issued no warnings.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    You allocated the first struct object on the stack in linecount() and func2() writes over that memory region.

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    17
    how should i get access of that structure that i have filled in the first function.Do i have to make the structure global ?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    int main()
    {
       struct string1 *string_var2;
       char temp;
       string_var2 = linecount();
       temp=string_var2->line1[0]; //Dereferncing works fine here.
       func2(string_var2)
    }
    
    struct string1 *linecount()
    {
        return malloc(sizeof struct string1);
    }
    
    void func2(struct string1 *num)
    {
       //when i defernece the pointer iam getting junk values.
       char mychar;
       mychar=num->line1[0];
    }

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int main()
    {
        struct string1* string_var2 = linecount();
        char temp = string_var2->line1[0]; // Dereferencing works fine here.
        func2(string_var2)
        free(string_var2);
    }
    
    struct string1* linecount()
    {
         return malloc( sizeof(struct string1) );
    }
    
    void func2(struct string1* num)
    {
        //when i defernece the pointer iam getting junk values.
        char mychar = num->line1[0];
    }
    You MUST free what you allocate!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  2. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  3. Structure containing a union of more structures!
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 03-08-2003, 11:53 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. error message, what does it mean?
    By Shy_girl_311 in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 09:54 PM