Thread: How do I assign an array to a set of GtkWidget * in C?

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    11

    How do I assign an array to a set of GtkWidget * in C?

    I'm not inexperienced in C, but this one has me baffled. It involves GTK+3.0.

    The relevant bits of code are:

    Code:
    char FlagEnt[NFLAGS][8] = ...
    
    static GtkWidget *  flentry[NFLAGS];
    static GtkWidget *  sflentry;
    
     for (i = 0; i < NFLAGS; i++)
    {
        sflentry = GTK_WIDGET(gtk_builder_get_object(builder, FlagEnt[i]));
        flentry[i] = sflentry;
    }
    
    Now this compiles OK, but doesn't behave as I expected.
    Code:
    (gdb) p sflentry
    $2 = 0x555555aaea00
    (gdb) p &sflentry
    $4 = (GtkWidget **) 0x5555557bc1e0 <sflentry>
    (gdb) p *sflentry
    $5 = {parent_instance = {g_type_instance = {g_class = 0x5555558e80d0}, ref_count = 1, qdata = 0x555555aadfc0}, priv = 0x555555aae910}
    (gdb) p flentry[7]
    $6 = 0x0
    (gdb) p &flentry[7]
    $7 = (GtkWidget **) 0x5555557cdbd8 <flentry+56>
    

    Where I was expecting flentry[7] to be the same as sflentry.

    I've run a test program using int * to check my understanding of pointers in this context:
    Code:
    #include <stdio.h>
    
    int * testc[3];
    
    int main()
    {
      int * m;
      int x = 10;
    
      m = &x;
      testc[0] = m;
    
      printf("%p %p %d\n", m, testc[0], *testc[0]);
    
      return 0;
    }
    

    And all seems fine.

    What am I missing? Am I just being dense?

    If anyone wants any more info, just let me know.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You haven't told us what NFLAGS is.

    You also haven't told us exactly where you put the breakpoint for the debugging output you show.
    Last edited by john.c; 04-12-2020 at 08:45 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    11
    Quote Originally Posted by john.c View Post
    You haven't told us what NFLAGS is.

    You also haven't told us exactly where you put the breakpoint for the debugging output you show.
    #define NFLAGS 8
    The breakpoint is just after the assignment loop.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Where exactly is "just after" the assignment loop.
    Do you mean the line with the closing brace?
    Or a line after that?
    If it was the line with the closing brace, try putting it on the next statement.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    Registered User
    Join Date
    Apr 2020
    Posts
    11
    Quote Originally Posted by john.c View Post
    Where exactly is "just after" the assignment loop.
    Do you mean the line with the closing brace?
    Or a line after that?
    If it was the line with the closing brace, try putting it on the next statement.
    No, I meant what I said - AFTER the assignment loop, in other words after the closing brace.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Good luck with that.
    From what you've shown your result is impossible.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    Registered User
    Join Date
    Apr 2020
    Posts
    11
    Quote Originally Posted by john.c View Post
    Good luck with that.
    From what you've shown your result is impossible.
    Oh, come on!

    The code and results are exactly as I displayed them. Saying the results are impossible isn't helpful. They are what they are - I haven't made them up.

    I'm beginning to wonder if I have stumbled on a gcc bug. I'll try another compiler.

  8. #8
    Registered User
    Join Date
    Apr 2020
    Posts
    11
    Quote Originally Posted by clancyj4 View Post
    Oh, come on!

    The code and results are exactly as I displayed them. Saying the results are impossible isn't helpful. They are what they are - I haven't made them up.

    I'm beginning to wonder if I have stumbled on a gcc bug. I'll try another compiler.
    Nope, clang does the same. It must be me...

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Whatever, buddy.
    If there was a better answer to your question then why aren't the people far more knowledgeable than me answering it?

    I'm beginning to wonder if I have stumbled on a gcc bug.
    The common refrain of the bad programmer.
    Last edited by john.c; 04-13-2020 at 10:53 AM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    Apr 2020
    Posts
    11
    Quote Originally Posted by john.c View Post
    Whatever, buddy.
    If there was a better answer to your question then why aren't the people far more knowledgeable than me answering it?


    The common refrain of the bad programmer.
    Ah, abuse now. Look at my last post.

    Really, if you can't say anything constructive, please STFU.

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    You're the POS who started it with your snarky replies.
    We've seen people with your mental disease before.
    It's not possible to help you until you get psychiatric help.
    But of course you won't do that, because you're perfect.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  12. #12
    Registered User
    Join Date
    Apr 2020
    Posts
    11
    Can anyone tell me what the complaints procedure is against this sort of abuse? Do the members here really want to be remembered for this type of behaviour?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by clancyj4
    The code and results are exactly as I displayed them. Saying the results are impossible isn't helpful. They are what they are - I haven't made them up.
    Yeah, but it's far more likely that you've made a mistake elsewhere than for it to be a compiler bug, and because you've only posted a code snippet for which the logic appears to be as to say, as far as everyone else is concerned it really is impossible. So while john.c had an extremely rude way of putting it, it is true that it is unwise to talk about a compiler bug before verifying that your code is correct, and you did quickly discover through another compiler that it is unlikely to be a compiler bug.

    What I suggest is this: come up with the smallest and simplest compilable program that demonstrates the problem. This should be a program that other people can compile and run for themselves, preferably without needing GTK. Chances are, in the process of simplifying your code to come up with that bare bones example program, you will find that you did indeed make a mistake in code that you didn't show us here.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Apr 2020
    Posts
    11
    Quote Originally Posted by laserlight View Post
    Yeah, but it's far more likely that you've made a mistake elsewhere than for it to be a compiler bug, and because you've only posted a code snippet for which the logic appears to be as to say, as far as everyone else is concerned it really is impossible. So while john.c had an extremely rude way of putting it, it is true that it is unwise to talk about a compiler bug before verifying that your code is correct, and you did quickly discover through another compiler that it is unlikely to be a compiler bug.

    What I suggest is this: come up with the smallest and simplest compilable program that demonstrates the problem. This should be a program that other people can compile and run for themselves, preferably without needing GTK. Chances are, in the process of simplifying your code to come up with that bare bones example program, you will find that you did indeed make a mistake in code that you didn't show us here.
    True, the compiler bug thought was just clutching at straws.

    I did try to replicate the problem in a simpler form, but it worked as expected. Maybe it is too simple, so I'll try with a more complex example.

    Finally, you say that my code snippets are impossible. I just can't see the problem - please would you explain what's wrong?

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by clancyj4
    Finally, you say that my code snippets are impossible. I just can't see the problem - please would you explain what's wrong?
    Not your code snippets; the result you observed given the code snippet. After all, it was this unexpected result that led you here, and indeed given the logic expressed by your code snippet, what you observed does sound impossible.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. assign values 2d array to normal array
    By Gertjan Haan in forum C Programming
    Replies: 1
    Last Post: 10-29-2015, 11:10 PM
  2. assign string to int array
    By lilostitch382 in forum C++ Programming
    Replies: 10
    Last Post: 02-22-2010, 06:48 PM
  3. Assign array to another.
    By boblettoj99 in forum C Programming
    Replies: 8
    Last Post: 12-03-2009, 04:32 PM
  4. how to assign value in two -dimensional array?
    By zcrself in forum C Programming
    Replies: 7
    Last Post: 11-26-2009, 11:00 PM
  5. assign value of pointer array
    By zcrself in forum C Programming
    Replies: 12
    Last Post: 08-17-2009, 02:12 PM

Tags for this Thread