Question about arrays

Question about arrays…

Hi,

I have a C++ Class with the following:
Int8 Playfield[3][3];

Later in my program I wish to initialize the above 2 dimensional array with
the following values:
0 / 1 / 0
1 / 1 / 1
0 / 1 / 0

I tried the following, but it does not compile:

Playfield
= { 0, 1, 0,
1, 1, 1,
0, 1, 1 };

Can someone tell me how to do the above correctly?

Thanks in advance!

Jesse
SLNTHERO at AOL.com (mailto:SLNTHERO at AOL.com)
http://www.SilentHeroProductions.com (http://www.SilentHeroProductions.com)

I believe the only time you can initialize an array like that is at the
time you’re declaring it. Otherwise, you have to address each element
individually. That’s in straight C/C++. If the array is a class member
I’m not sure you can even do that. The only way I could see that being
done at declaration time is if the array was a static member of the
class. But you may not want that for this particular class.–
Lilith

On 2/9/2007 at 12:59 PM, wrote:
Question about arrays…

Hi,

I have a C++ Class with the following:
Int8 Playfield[3][3];

Later in my program I wish to initialize the above 2 dimensional
array with

the following values:
0 / 1 / 0
1 / 1 / 1
0 / 1 / 0

I tried the following, but it does not compile:

Playfield
= { 0, 1, 0,
1, 1, 1,
0, 1, 1 };

Can someone tell me how to do the above correctly?

Thanks in advance!

Jesse
SLNTHERO at AOL.com (mailto:SLNTHERO at AOL.com)
http://www.SilentHeroProductions.com
(http://www.SilentHeroProductions.com)

Hi,

maybe this works:

Playfield = { “0, 1, 0”, “1, 1, 1”, “0, 1, 1”};

but i?m not sure if you can initialize the array in the way like this at
runtime. As far as i know this way only works at the definition.

like:

Int8 Playfield[3][3] = { “0, 1, 0”, “1, 1, 1”, “0, 1, 1”};

but this is only smattering because i?m a C++ beginner.

Alex

P.S. So this is my first post on a mailinglist please be clement if a do
anything wrong.

2007/2/9, SlntHero at aol.com :>

Question about arrays…

Hi,

I have a C++ Class with the following:
Int8 Playfield[3][3];

Later in my program I wish to initialize the above 2 dimensional array
with the following values:
0 / 1 / 0
1 / 1 / 1
0 / 1 / 0

I tried the following, but it does not compile:

Playfield
= { 0, 1, 0,
1, 1, 1,
0, 1, 1 };

Can someone tell me how to do the above correctly?

Thanks in advance!

Jesse
SLNTHERO at AOL.com
http://www.SilentHeroProductions.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SlntHero at aol.com wrote:

Question about arrays…

Hi,

I have a C++ Class with the following:
Int8 Playfield[3][3];

Later in my program I wish to initialize the above 2 dimensional array with
the following values:
0 / 1 / 0
1 / 1 / 1
0 / 1 / 0

I tried the following, but it does not compile:

Playfield
= { 0, 1, 0,
1, 1, 1,
0, 1, 1 };

Can someone tell me how to do the above correctly?

Thanks in advance!

Jesse
SLNTHERO at AOL.com (mailto:SLNTHERO at AOL.com)
http://www.SilentHeroProductions.com (http://www.SilentHeroProductions.com)



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

this is not an sdl question.

int stuff[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

also, is Int8 part of sdl? i thought Uint8 was. anyway, i just
include stdint.h and use uint8_t or int_t.

matt

matt wrote:

SlntHero at aol.com wrote:

Question about arrays…

Hi,

I have a C++ Class with the following:
Int8 Playfield[3][3];

Later in my program I wish to initialize the above 2 dimensional array with
the following values:
0 / 1 / 0
1 / 1 / 1
0 / 1 / 0

I tried the following, but it does not compile:

Playfield
= { 0, 1, 0,
1, 1, 1,
0, 1, 1 };

Can someone tell me how to do the above correctly?

Thanks in advance!

Jesse
SLNTHERO at AOL.com (mailto:SLNTHERO at AOL.com)
http://www.SilentHeroProductions.com (http://www.SilentHeroProductions.com)



SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

this is not an sdl question.

int stuff[3][3] = {{1,2,3},{4,5,6},{7,8,9}};

also, is Int8 part of sdl? i thought Uint8 was. anyway, i just
include stdint.h and use uint8_t or int_t.

matt

i just tried that without the extra curly braces and it still compiled
but gave a warning. i only code i C not C++ so i cant help with that.

matt

P.S. So this is my first post on a mailinglist please be clement if
a do anything wrong.

And this response is offered entirely in the spirit of offering
helpful advice. So I hope you won’t take it askance.

It’s often better to avoid posting answers to questions when you’re
not actually sure that you know the answer.

There are dozens of people subscribed to this mailing list. If you
think you might not know the answer to a question, it’s usually better
to wait for a day or two. Chances are, someone who does know will
chime in, and either you’ll get confirmation that you were right, or
else you’ll learn something new. Posting an answer with the caveat
that you’re just guessing can confuse the original poster, who’s
presumably already a little confused.

Of course, it’s a different matter when nobody else is sure what the
answer is either. If a couple of days go by and still nobody has
replied, then that’s a good time to put forth your best guess.

Alternately, if you’re not sure of your answer, sometimes you can
become sure by writing a short program to test your idea out. Then you
can post with confidence, and both of you will have learned something
new.

If you had run this code through a compiler, for example:

Int8 Playfield[3][3] = { “0, 1, 0”, “1, 1, 1”, “0, 1, 1”};

You would have gotten an error message from the compiler, and would
have realized that you used quotes instead of curly braces around your
inner initialization lists, and you would have had a chance to correct
your mistake.

b

Alexander Popp wrote:

Int8 Playfield[3][3] = { “0, 1, 0”, “1, 1, 1”, “0, 1, 1”};

those would be strings, not signed 8 bit integers.

matt

Question about arrays…

Just because the discussion started, let me introduce a little tip.

To manage our vector, we have something like this:

typedef struct {
Float32 v; /* Float 32, is just a 32 bit float, part of our lib,
same with Float16, Float64, Float80 and Float96. Note that only
Float32 and Float64 are standard in C other are proprietary of our
lib */
Uint8 d;
} Vector32;

/* Those things are our standard API for pseudo object programming in
C */
Vector32_alloc(Vector32 **v) {
*v = malloc(sizeof(Vector32));
}
Vector32_init(Vector32 *v) {
v->d = 0;
v->v = NULL;
}
Vector32_setDimension(Vector32 v, Uint8 d) {
if(!v || v->v) return;
v->d = d;
v->v = malloc(d * sizeof(Float32));
}
/
What is of some interest for you, maybe */
Vector32_setData(Vector32 *v, …) {
va_list l;
va_start(l, v);
char *p;
int i;
for(*p = v, i = 0; p && i < v->d / We also verify dimension, if
too much args are passed */; p++, i++) {
v->v[i] = va_arg(l, Float32);
}
va_end(l);
}

I did not copy past that code, I wrote it on the fly, so do not blame
me if a little typo inserted itself somewhere. It’s just for the
concept. You can apply the setData function to anything.

RegardsOn Feb 9, 2007, at 7:59 PM, SlntHero at aol.com wrote:

Kuon
Programmer and sysadmin.

“Computers should not stop working when the users’ brain does.”

-------------- next part --------------
A non-text attachment was scrubbed…
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2434 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070210/8a6d80d4/attachment.bin