Pointer confused... :-(

This is a discussion on Pointer confused... :-( within the C Programming forums, part of the General Programming Boards category; Hello all, I have a problem with some pointers. I am very new to C ... I have a function ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    3

    Pointer confused... :-(

    Hello all,

    I have a problem with some pointers. I am very new to C ...

    I have a function "open_project_file", this function initializes a struct called ZConf. open_project_file should now call an external function whicht should read the project data from XML and asign the content to my struct.

    Here is my dummy code:

    Code:
    typedef struct _ZConf {
        gchar *projectname;
        gchar *projectpath;
    } ZConf;
    
    int open_project_file() {
        ZConf settings;
        read_xml(&settings);
    
        /* this line does not work 
       !!!! i don't can access the data in the structure!!!!*/
        printf(settings.projectname);
        printf(settings.projectpath);
    }
    
    void read_xml(ZConf *settings) {
        /* does some file integrity checks */
        /* then calls: */
        parse_xml(settings);
    }
    
    void parse_xml(ZConf *settings) {
        /*parse the XML file and insert values to the structure */
        settings->projectname = "Damn_pointers";
        settings->projectpath = "/home/hanez/projects";
    }
    Sorry, when not described perfect but the original code is much more complex and i don't want to post all the files. I think you could the understand the problem anyway... I know you can because you know how to hack in C... :-)

    I would be very glad if someone could help me out.

    Kind regards,
    Johannes - hanez.org

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by hanez
    Hello all,

    I have a problem with some pointers. I am very new to C ...

    S the original code is much more complex and i don't want to post all the files
    Well, there is nothing wrong with the functions you posted, except you forgot to give a return value in open_project_file().

    Breaking the project down into a minimal set of functions that exhibits the problem is a pretty good path to debugging, however...


    If you compile and execute just the lines that you posted (and a typedef for gchar and a main() that invokes open_project_file()), what do you get? I get the expected printout.

    If these functions don't give you the expected printout in open_project_file(), then put printf statements in the other functions to see where it gets broken.

    Regards,

    Dave

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    typedef struct _ZConf {
        gchar *projectname;
        gchar *projectpath;
    } ZConf;
    
    int open_project_file() {
        ZConf settings;
        read_xml(&settings);
    
        /* this line does not work 
       !!!! i don't can access the data in the structure!!!!*/
        printf(settings.projectname);
        printf(settings.projectpath);
    What's a gchar? It had better be the same thing as a char, or this call to printf isn't going to work. Also, you usually want to not just pass something as the first argument to printf that isn't a specifically set control string. Otherwise, the user could fill 'projectname' with a string which has embedded % specifiers and printf would be expecting more to follow just the single argument.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    3

    Wink

    What's a gchar? It had better be the same thing as a char, or this call to printf isn't going to work. Also, you usually want to not just pass something as the first argument to printf that isn't a specifically set control string. Otherwise, the user could fill 'projectname' with a string which has embedded % specifiers and printf would be expecting more to follow just the single argument.
    Thanks for reply!

    I am using Glib from the GNOME project and a gchar is wrapped char. It is the same type. Sorry for my fault in printf. I know the format method of strings but forgot to write them teh right way in my example. Sorry!

    My mainproblem is, that i don't can access the data in the structure. I am reading the code at the monet and i will follow up with more information when i know. Maybe i will post the original code but there are many files and i don't want to do that here. I will write a better example that not works on my machine anyway when i don't find the error within the next hour...

    Regards,
    Johannes

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > I think you could the understand the problem anyway
    Yeah, but there are so many ways of interpreting vague posts, which usually end up as wild guesses and frustration on both sides.

    It's usually a lot better if you begin by reducing the problem to a small program which demonstrates the problem (as you see it), which you can the post in it's entirety. Not only are you likely to find out the answer to your question, if you don't, we'll be in a much better position to help you when you post your small complete program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    3
    Hello,

    thanks to all of you. It helped me thinking in another way.

    I am very new to C and this was my first post. Sorry for the dump question. The code i originally posted is running with the elementary fixes on my system. My problem was, that i had many more files with many things that are done before making use of the vars in the struct. Anywhere in this construct of functions i have made a free command in the XML parser and this free comand has deleted the content of my pointers in the struct.

    Just, you know my fault now.

    Sorry again and thanks! I will come back again with better declared problems... ;-)

    Have a nice day...
    Johannes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-15-2009, 08:38 AM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM

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