Thread: Structure pointer help

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    3

    Structure pointer help

    How would I go about doing this? I have a structure pointer and inside the structure is a pointer to a char. Here is what I have sofar, but it crashes.

    Code:
    #include <stdio.h>
    
    char doug[] = "Doug";
    
    struct structure
    {
    	char* name;
    }*struct_ptr;
    
    int main()
    {
        struct_ptr->name = &doug[0]; // This causes the freezing
        printf("&#37;s",*struct_ptr->name);
        return 0;
    }

    I know that the problem is the assigning of the address of the string to the structure, but I don't know how to fix that. Thanks in advance.

    I searched and searched google, this site, and other references, but I haven't come across how to assign the address of a char to a member exactly like the above example.
    Last edited by Papercut1986; 09-03-2007 at 06:31 PM.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The line you think is causing the issue isn't the real problem. Lose the '*' char in the printf() statement.

    Nice name, and welcome to cboard.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    struct_ptr isn't pointing to anything.
    Last edited by Doodle77; 09-03-2007 at 06:51 PM.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    Quote Originally Posted by MacGyver View Post
    The line you think is causing the issue isn't the real problem. Lose the '*' char in the printf() statement.

    Nice name, and welcome to cboard.
    Thank you for the help

    However, I am sure that it is the line because it still freezes after I removed the call to printf
    I had just added the printf to the code to clarify to the people reading the post what was going on . Plus, wouldn't I need the dereferencing operator or it would try to print the address in the pointer as a string instead of what it points to?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    struct s
    { int a; };
    
    int main()
    {
        struct s my_s;
        struct s *sp;
        sp = &my_s;
    
        (*sp).a; // is the same thing as
        sp->a;
        return 0;
    }
    Code:
    #include <stdio.h>
    
    char doug[] = "Doug";
    
    struct structure
    {
        char* name;
    }*struct_ptr;
    
    int main()
    {
        struct_ptr->name = &doug[0]; // struct_ptr is not pointing to anything in particular.
        printf("&#37;s",*struct_ptr->name); // use "printf("%s", struct_ptr->name);". -> already dereferences struct_ptr.
        return 0;
    }
    Code:
    #include <stdio.h>
    
    char doug[] = "Doug";
    
    struct structure
    {
        char* name;
    } *struct_ptr;
    
    int main()
    {
        struct structure sinstance; // something for struct_ptr to point to
        struct_ptr = &sinstance;
        struct_ptr->name = doug; // doug means the same thing as &doug[0]. sinstance.name now equals "Doug".
        printf("%s",struct_ptr->name);
        return 0;
    }
    Last edited by robwhit; 09-03-2007 at 07:03 PM.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Unbelievable, I can't believe I missed that.

    /me goes to cry in the corner.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    3
    Thank you for all the help Macgyver, doodle77, and robwhit

    Helps alot and should help me learn more about structure pointers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. method returning a pointer to a structure
    By nacho4d in forum C++ Programming
    Replies: 3
    Last Post: 05-25-2009, 10:01 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 08:59 AM
  4. Pointer to a structure
    By frenchfry164 in forum C Programming
    Replies: 5
    Last Post: 03-16-2002, 06:35 PM
  5. Pointer to structure problem
    By unregistered in forum C Programming
    Replies: 3
    Last Post: 12-24-2001, 07:54 AM