viewing declared struct objects using gdb

This is a discussion on viewing declared struct objects using gdb within the Linux Programming forums, part of the Platform Specific Boards category; lets say I have a program resembling: Code: #include <stdio.h> struct blah { int id; char* user; }; int main() ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    viewing declared struct objects using gdb

    lets say I have a program resembling:
    Code:
    #include <stdio.h>
    struct blah
    {
    	int id;
    	char* user;
    };
    
    int main()
    {
    	struct blah mystrct;
    	mystrct.id = 1;
    	mystrct.user = "hi there\n";
    	fprintf(stdout,mystrct.user);
    	return 0;
    }
    And I am trying to debug this with GDB, how could I view the variable
    mystrct.user or mystrc.id
    assuming I have a breakpoint at fprintf.
    when typing print mystrct or mystrct.user it says cannot view struct objects or something.
    Would I have to pick apart the stack?

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    err
    hehehe-
    ^_^

    Turns out that the part I was trying to debug wouldn't have helped anyway, I did this stupid thing earlier which caused a seg-fault:

    Code:
    fp = fopen("somefile.txt","rw");
    fprintf(fp,"blah");
    fclose(fp);
    ooh boy, that was fun, I thought the whole time it was with a struct's string and a buffer overflow...
    I can't believe I made that mistake either, but thanks for the help, I'll use that when I need to. But, I couldn't find out how to display members of a struct. Oh well-

    -LC
    Last edited by Lynux-Penguin; 07-20-2003 at 06:49 PM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 08:45 PM

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