Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » struct

This thread is locked; no one can reply to it. rss feed Print
struct
Albin Engström
Member #8,110
December 2006
avatar

mm, having a little problem here:

making an interface for 'a program' and i am using dynamic values to change the color of the text. for some reason i can use global values but not those stored in structures... this is the messege i get when compiling: "expected primary-expression before '.' token"..

NOT WORKING CODE PIECE:
textprintf_ex(buffer, font, xpos+((width/2)-7),ypos+height-15, makecol(game.graphics._interface.textcol1, 255, 255), -1 , "OK");

WORKING CODE PIECE:
textprintf_ex(buffer, font, xpos+((width/2)-7),ypos+height-15, makecol(muu(global), 255, 255), -1 , "OK"); used as a test...

Why god, why?!

(i have a feeling i'm missing something very .. basic..)

miran
Member #2,407
June 2002

Post more information.

--
sig used to be here

HoHo
Member #4,534
April 2004
avatar

Are any of those structures or parts of the structure allocated dynamically? If yes then use "->" instead of "."

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Marco Radaelli
Member #3,028
December 2002
avatar

Well, apart from the missing bracket
textprintf_ex(buffer, font, xpos+((width/2)-7),ypos+height-15, makecol(game.graphics._interface.textcol(1, 255, 255), -1 , "OK");

What's textcol? A pointer to function?

miran
Member #2,407
June 2002

@Marco: textcol1 is obviously the R component of the colour. So you're wrong. :P

--
sig used to be here

Albin Engström
Member #8,110
December 2006
avatar

allocated dynamically???

the missing braclet wasn't there in the original code piece :P..

more information... like?

makecol1: int makecol1;

miran
Member #2,407
June 2002

More information like how and where those structs are declared and how the actual objects are created.

--
sig used to be here

Albin Engström
Member #8,110
December 2006
avatar

ouch.. this is going to be painful..

i declare my structures like this:

struct game
{
  struct graphics
  {
    struct _interface
    {
    int textcol1;
    ...

    }_interface;
  }graphics;
}game;

- - he?
"how the objects where created"???

miran
Member #2,407
June 2002

And which language is this in? C or C++? Is this code in a header?

--
sig used to be here

Albin Engström
Member #8,110
December 2006
avatar

C++ - Not a header. (i put everything into the same file..) - -

miran
Member #2,407
June 2002

The following code compiles without problems when I put it in a file called test.cpp:

1struct game
2{
3 struct graphics
4 {
5 struct _interface
6 {
7 int textcol1;
8 }_interface;
9 }graphics;
10}game;
11 
12int main() {
13 game.graphics._interface.textcol1 = 123;
14 return 0;
15}

Can you write a similar example that doesn't compile?

--
sig used to be here

tobing
Member #5,213
November 2004
avatar

In your code, game, graphics and _interface are structures, not instances. So you can't write "game.graphics._interface.textcol1".

What you CAN do is: have an instance of type game::graphics::_interface, say ifc, then use ifc.textcol1. Code:

game::graphics::_interface ifc;
ifc.textcol1 = 45;

Edit: Oops, I didn't see the names following the struct definitions. So that means then that the structure names are also at the same time names for the corresponding members. Urgs. I'm not used to code like this...

Albin Engström
Member #8,110
December 2006
avatar

The following code.. works.

1#include <iostream>
2 
3using namespace std;
4 
5struct game
6{
7 struct graphics
8 {
9 struct _interface
10 {
11 int textcol1;
12 }_interface;
13 }graphics;
14}game;
15 
16int main()
17{
18 cout << game.graphics._interface.textcol1;
19 system("PAUSE");
20}

did some more research and turns out that i have no trouble doing this:
using "game.graphics._interface.textcol1" in a function outside a structure and.. (i don't know the name for it.. declared? no.. the code stuff..) anyway using it inside a function in a structure gets me that code error... that's the difference.. :S.

1#include <iostream>
2 
3using namespace std;
4 
5struct game
6{
7 struct graphics
8 {
9 struct _interface
10 {
11 int textcol1;
12 }_interface;
13 }graphics;
14 
15 struct blabla
16 {
17 void function()
18 {
19 line(USING GAME.GRAPHICS._INTERFACE.TEXTCOL1!!!)
20 }
21 }blabla;
22}game;
23 
24int main()
25{
26 game.blabla.function();
27 system("PAUSE");
28}

This Code Should not work.. (i didn't compile it due to this incredible crappy computer here in school (and the others).. takes abuot 4min compiling this shit - -).

"not used to this kind of code..." it means it sucks right ^^'? i really need to polish(eer?) my coding..

miran
Member #2,407
June 2002

Wel duh! Of course it doesn't compile. You can't use an object before it is created.

As a sidenote, it would probably not a bad idea for you to read a book about programming in C++...

--
sig used to be here

GullRaDriel
Member #3,861
September 2003
avatar

Well, perhaps using this ...
Hmm ha, heh, you should really follow Miran advice.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

miran
Member #2,407
June 2002

No, he wants to use the global object named game that is of type game. Which leads me to beleive he doesn't even know what he's doing...

--
sig used to be here

Albin Engström
Member #8,110
December 2006
avatar

><... thanks guys :).. i don't have a book.. and i don't know where to get one that isn't out of date - -.. using this site: "functionx.com" very good.. i think.. i don't read it as much as i need thought..

been using structures like this for a long time without problems.. kinda felt like i used them the wrong way..

apparently... i don't know what i'm doing.. could you tell me how structures should be used then? or direct me to a link so that i wont waste your precious time :)

GullRaDriel
Member #3,861
September 2003
avatar

Well, all done ! Albin can now correct his problem.

EDIT: The C way I am using. Can help. Do not take care of what the function do but on how struct is used (and how you access the new created type)

n_strlist.h

1typedef struct N_STR
2 {
3 
4 /*! the string */
5 char *data;
6 
7 /*! lenght of string (in case we wanna keep information after the 0 end of string value) */
8 int length;
9 
10 /*! pointer to the next item of the list */
11 
12 struct N_STR *next;
13 /*! pointer to the previous item of the list */
14 
15 struct N_STR *prev;
16 
17 }
18 
19N_STR;
20 
21 
22 
23/*!
24 * A doubly linked list prototype of N_STR item
25 */
26 
27typedef struct N_STRLIST
28 {
29 
30 N_STR /*!start of list*/
31 *start,
32 /*!end of list*/
33 *end;
34 
35 int /*!number of item*/
36 nb_item,
37 /*!maximum of item in the list, -1 for undeterminated*/
38 nb_max_item;
39 
40 }
41 
42N_STRLIST;

n_strlist.c

1#include "n_strlist.h"
2 
3 
4 
5/*!\fn int init_list( N_STRLIST **LIST , int max )
6 *
7 *\brief Always apply this function to any N_STRLIST *object before first use
8 *
9 *\param LIST an N_STRLIST object
10 *\param max an integer to assign 'max' item limitation to N_STRLIST **LIST, -1 for undeterminated
11 *
12 *\return TRUE on success, FALSE on failure (generaly due to memory lake, report is written to stderr)
13 */
14 
15int init_list( N_STRLIST **LIST , int max )
16 {
17 
18 /*point to nothing*/
19 *LIST = NULL;
20 
21 /* allocating memory, returning FALSE if impossible*/
22 Malloc( *LIST , N_STRLIST , 1 );
23 
24 if ( !( *LIST ) )
25 return FALSE;
26 
27 
28 /* initilize list elements*/
29 
30 /*there is no item in the list*/
31 ( *LIST ) -> nb_item = 0 ;
32 
33 /* we want 'max' element in our list*/
34 ( *LIST ) -> nb_max_item = max;
35 
36 /* after allocation, we already have no item so we point nowhere*/
37 ( *LIST ) -> start = ( *LIST ) -> end = NULL;
38 
39 /*returning success of allocation*/
40 return TRUE;
41 
42 } /*init_list(...)*/
43 
44 
45 
46/*!\fn int add_first( N_STRLIST *LIST , char *item , int length )
47 *
48 *\brief use this to add a string item at the begin of your N_STRLIST *list
49 *
50 *\param LIST an N_STRLIST *object
51 *\param item a char *string
52 *\param length an int to specify the real length we wanna use for this string
53 *
54 *\return TRUE on success, FALSE on failure (generaly due to memory lake)
55 */
56 
57int add_first( N_STRLIST *LIST , char *item , int length )
58 {
59 
60 N_STR * newstr;
61 
62 if ( LIST != NULL ) /*it's an active list*/
63 {
64 
65 /*checking if full or undeterminated*/
66 
67 if ( LIST-> nb_max_item != -1 && ( LIST -> nb_item + 1 >= LIST -> nb_max_item ) )
68 return FALSE;
69 
70 if ( LIST -> nb_item == 0 ) /*it's an empty list we add the root*/
71 {
72 
73 Malloc ( LIST->start, N_STR , 1 ); /*memory allocation*/
74 
75 LIST -> start -> next = LIST -> start -> prev = NULL; /*initialize LIST*/
76 
77 Malloc ( LIST -> start -> data, char , length ); /*allocating data array*/
78 Strcpy( item , LIST -> start -> data , length ); /*filling data array*/
79 
80 LIST -> start -> length = length ; /*saving the length of our string*/
81 LIST -> end = LIST -> start; /*first node is OK now*/
82 
83 }
84 else
85 {
86 /*create new node*/
87 Malloc( newstr , N_STR , 1 );
88 Malloc( newstr -> data , char , length );
89 /*modify the next*/
90 newstr -> next = LIST -> start;
91 /*copying the string*/
92 Strcpy( item , newstr->data , length );
93 newstr->length=length;
94 /*modify the new next node*/
95 LIST -> start -> prev = newstr;
96 /*moving to the new start position*/
97 LIST -> start = newstr;
98 }
99 
100 }
101 else
102 return FALSE; /*that was not an initialized list*/
103 
104 
105 LIST -> nb_item = LIST -> nb_item + 1; /*here we have a new element in*/
106 
107 return TRUE; /*successful*/
108 
109 } /*add_first(...)*/

But I think you want C++ and I am bored to do more than a simple copy paste from already written code.

;D

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

miran
Member #2,407
June 2002

Quote:

or direct me to a link so that i wont waste your precious time

http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html

There should also be a book on plain C there somewhere. Structs are probably explained in it, while the one on C++ only focuses on classes (although the difference between a class and a struct is very small).

--
sig used to be here

GullRaDriel
Member #3,861
September 2003
avatar

Albin said:

i wont waste your precious time

Not really, it is 150$/Hour for each, Miran and I. Every started hour is fully accounted.

VISA,CB,MASTERCARD,Travelers check accepted.

;D

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Neil Black
Member #7,867
October 2006
avatar

Well then I owe you guys, like, $5,000 each.
;D

Albin Engström
Member #8,110
December 2006
avatar

^^'

Marco Radaelli
Member #3,028
December 2002
avatar

Quote:

@Marco: textcol1 is obviously the R component of the colour. So you're wrong. :P

Yeah, sorry :-/

Go to: