Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

looping // structures // arrays // pointers :P

Options
  • 30-03-2009 1:39am
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    ok i have a two dimensional structure, that has two parts.
    #define STRUCT_SIZE = 5
    
    struct node
    {
    	int num;
    	char* colour;
    }; 
    
    struct node nodes[STRUCT_SIZE][STRUCT_SIZE];
    
    now what i'm trying to do is loop through the array of nodes and store the number of the node into an array, if the colour member of the structure is black. since i don't know how many nodes will be black i need to set up a pointer.
    
    //pointer to hold values
    int *black_nums;
     
    for(i = 0 ; i < STRUCT_SIZE ; i++)
    	{
    		for(j = 0 ; j < STRUCT_SIZE; j++)
    		{
    			if(node[i][j].colour == NULL)
    				break;
    			
    			if(strcmp(nodes[i][j].colour,"black") == 0)
    			{
    				printf("%d\n", nodes[i][j].num);
                                    *(black_nums+i) = nodes[i][j].num;
    			}
    		}
    	}
    

    the output i get from this is completely random numbers though but the first 1-2 elements will be correct, the printf() will however print out the correct numbers that i want so it's just how i'm trying to store them is going wrong.

    i know it's probably something stupid but i can't get my head round it, spent all day coding and my brain hurts :pac:


Comments

  • Registered Users Posts: 3,945 ✭✭✭Anima


    [PHP]int *black_nums;[/PHP]
    [PHP]*(black_nums+i) = nodes[j].num;[/PHP]

    That doesn't look right to me but I'm also tired so I dunno :)


  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Anima wrote: »
    [PHP]int *black_nums;[/PHP]
    [PHP]*(black_nums+i) = nodes[j].num;[/PHP]

    That doesn't look right to me but I'm also tired so I dunno :)
    +1

    You are not initialising the pointer black_nums to anywhere. Its pointing at random memory.
    I'm guessing you are forgetting to malloc first.

    You can then realloc every time you find a new black


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    ahh of course,

    damn me running out of caffeine based substances last night :D.

    changed it to this and it's working.
    //pointer to hold values
    int *black_nums;
    black_nums = malloc(sizeof(int)); 
    int k = 0;
    
    for(i = 0 ; i < STRUCT_SIZE ; i++)
    	{
    		for(j = 0 ; j < STRUCT_SIZE; j++)
    		{
    			if(node[i][j].colour == NULL)
    				break;
    			
    			if(strcmp(nodes[i][j].colour,"black") == 0)
    			{
                                    realloc(black_nums, sizeof(int));
    				*(black_nums+k) = nodes[i][j].num;
                                    k++;
    			}
    		}
    	}
    

    now i have another problem, i know this is elementary programming but i can't for the life of my figure it out.

    i have another for loop like below. what i'm trying to do is print out a table of 200 numbers 1-200, in green text if it's not in the numbers array that i initialised above otherwise if the number is in the numbers array i want to print it red.

    the code below fills out the table correctly it's just the colouring that goes wrong.

    int block_num = 1;
    k = 0 ; //iterator used for numbers array.
    for (i = 0; i < 20; i++) 
    	{ 
    		for (j = 0; j < 10; j++) 
    		{
    			sprintf(text, "%d",block_num);
    			labels[i][j] = gtk_label_new_with_mnemonic(text); 
    			
    			if(block_num == *(numbers+k))
    			{
    				gdk_color_parse("red", &colour);
    			}	
    			else
    			{
    				gdk_color_parse("green", &colour);
    			}
    			
    			gtk_widget_modify_fg(labels[i][j], GTK_STATE_NORMAL, &colour);
    			
    			gtk_table_attach_defaults (GTK_TABLE (table), labels[i][j], 
    									   j, (j+1), i, (i+1));
    			
    
    			block_num++;
                            k++;
    		}
    	}
    


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    [PHP]int block_num = 0;
    //k = 0 ; //iterator used for numbers array.
    for (i = 0; i < 20; i++)
    {
    for (j = 0; j < 10; j++)
    {
    sprintf(text, "%d",block_num+1);
    labels[j] = gtk_label_new_with_mnemonic(text);

    if( (block_num+1) == numbers[block_num]; //*(numbers+k))
    {
    gdk_color_parse("red", &colour);
    }
    else
    {
    gdk_color_parse("green", &colour);
    }

    gtk_widget_modify_fg(labels[j], GTK_STATE_NORMAL, &colour);

    gtk_table_attach_defaults (GTK_TABLE (table), labels[j],
    j, (j+1), i, (i+1));


    block_num++;
    //k++;
    }
    }[/PHP]

    haven't tested it, but would that work?

    EDIT:looking at it again, its no different..silly me.
    don't know tbh.


Advertisement