Page 1 of 1

C++ Array of structures. (n00b help)

Posted: Thu Mar 23, 2006 5:35 pm
by Chris Corbyn
OK excuse the n00biness here but I'm just learning (GNU) C++ here and there :oops:

I'm going to try converting some PHP code into C++ that I wrote (a tokenizer). I think I'm going to need an array that contains a fixed number of elements, each of which is a very simple structure. Maybe I need a linked list instead but I haven't quite worked that out yet :P

OK I know how to initialize an array of say, integers with a list of values:

Code: Select all

int foo[] = { 1, 2, 3 };
But how would I do that with a list of structures? (I need to assign values to the properties of the structure).

Code: Select all

#include <iostream>

using namespace std;

//A pattern and a 1 or a 0
// depending upon if the
// pattern is a regex
struct tokDef
{
	char *pattern;
	char *tokName;
	int re;
};

//The array we'll store our defintions in
tokDef defintions[] = {
    /** How can I add some values here? **/
};

int main()
{
	//
	return 0;
}
Meh, I do think I need a linked list... I'll start reading (again) :(

The array hold defintions for a load of tokens that in PHP I was doing with a multi-dimensional array like this:

Code: Select all

$tokenDefs = array(
    'TOKEN_ONE' => array('/\bfoo\d{2}/', 1),
    'TOKEN_TWO' => array('::', 0)
);
But in C++ a structure seems more sensible to keep the definition grouped closely (?).

Cheers if anyone can push me in the right direction :)

Posted: Thu Mar 23, 2006 5:47 pm
by Chris Corbyn
Awesome well of my own accord I attempted this which seems to work :oops:

Code: Select all

#include <iostream>

using namespace std;

struct tokDef
{
	char *name;
	char *pattern;
	int re;
};

//The array we'll store our defintions in
tokDef defintions[] = {
	{ strdup("one"), strdup("test"), 1 }
};

int main()
{
	cout << defintions[0].name << " :: " << defintions[0].pattern << " :: " << defintions[0].re << endl;
	return 0;
}
Have I fluked it and found a hacky nasty way to do it or am I on the right track? :)

Posted: Thu Mar 23, 2006 6:00 pm
by feyd
a bit hackish, but it works. :)

Posted: Fri Mar 24, 2006 2:12 am
by Chris Corbyn
feyd wrote:a bit hackish, but it works. :)
Cool thanks :)

Posted: Fri Mar 24, 2006 6:03 am
by timvw
Why don't you use stl::list instead? :)

Anyway, if you were looking for a nice tokenizer i suggest you have a look at Lex.

Posted: Fri Mar 24, 2006 6:15 am
by Chris Corbyn
timvw wrote:Why don't you use stl::list instead? :)

Anyway, if you were looking for a nice tokenizer i suggest you have a look at Lex.
I'm very new to all this :( I'll look at what stl::list is for thanks :)

As for lex I know about it but I just enjoy writing things like this myself since I'm a bit of a string-manipulation nerd.

On the subject of strings, how the heck do you compile a pattern with the GNU regex library? :oops: I'm reading the documentation and although I kind of get it I'm a little lost how re_compile_pattern() works.

Code: Select all

#include <iostream>
#include "regex/regex.h"

using namespace std;

int main()
{
        static struct re_pattern_buffer rxp;
        rxp.buffer = 0;
        rxp.allocated = 0;

        re_compile_pattern("/foo/i", 7, &rxp);
        
        return 0;
}
It just gives me this error:

Code: Select all

d11wtq@d11wtq real_stuff $ g++ lexer.cpp -o runme
/tmp/cchJC6Sz.o(.text+0x34): In function `main':
: undefined reference to `re_compile_pattern(char const*, int, re_pattern_buffer*)'
collect2: ld returned 1 exit status
The documentation can be found here: http://www.math.utah.edu/docs/info/regex_1.html#SEC6

I'm sure it'll all just "click" at some point :)

Posted: Fri Mar 24, 2006 1:10 pm
by Chris Corbyn
d11wtq wrote:On the subject of strings, how the heck do you compile a pattern with the GNU regex library? :oops: I'm reading the documentation and although I kind of get it I'm a little lost how re_compile_pattern() works.
Meh I gave up on that. Turns out it was pcre++/libpcre I needed anyway :)

Posted: Fri Mar 24, 2006 2:08 pm
by Chris Corbyn
Man I'm such a geek.... I'm actually getting excited because I'm writing regex in a new language.... meanwhile I'm guzzling a bottle of red before I go into town 8O

(Anyone want me to continue the running commentary? ... duh - duh - duh - derrrrr.... d11, this, is your life....)

Posted: Fri Mar 24, 2006 3:57 pm
by feyd
do continue. It's fun to watch someone descend into madness by talking to themselves on a board. :P

Posted: Fri Mar 24, 2006 4:50 pm
by Chris Corbyn
feyd wrote:do continue. It's fun to watch someone descend into madness by talking to themselves on a board. :P
Oh ok then :)

/notices slight stain from sloshed red wine on hand

Bah, I'm off out in 15 mins and ain't got a clue where I'm going. All I know is it's called Sankey's which is short for "Sankey Soap" and I'm going to see Krafty Kuts. If anyone knows where the hecks this place is (in Manchester, UK) do say so :D

Posted: Fri Mar 24, 2006 5:06 pm
by feyd

Posted: Fri Mar 24, 2006 10:50 pm
by Chris Corbyn
Wow! What an awesome night! That's all I'm going to say :D