By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

trying to construct an array.....

P: n/a
Hi all... I am trying to construct an 2x14 array that compares a given
character with the 1st column of data type "t" and returns the second
column's appropriate value "v". I've been trying this, but doesn't seem to
work.... Any idea of what I am missing?

void Detect_type(void)
{
volatile char r1tipus = *R1TIPUS;
unsigned char cadena [14] [2] = {

0x00 , stv1pk,
0x02, stv1rms,
0x04, itv1rms,
0x06, ian1pk,
0x08, ian1rms,
0x0A, i1pk,
0x0C, i1rms,
0x0E, vccoff,
0x10, vccon,
0x12, rearme,
0x14, magneto,
0x16, offmanual,
0x18, interno,
0x1A, remotein
};

int t;
char *v;

for (t=0; t <14; t++) cadena[t] = t;

if (cadena[t] == r1tipus *v =cadena[2])

return(v);
}
Cheers!!

Yodai

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hummm... I have just realized there's an error, so I correct myself: (still
doesn't work, though) the 2nd column should be as "stv1pk" to be a character
string:

void Detect_type(void)
{
volatile char r1tipus = *R1TIPUS;
unsigned char cadena [14] [2] = {

0x00 , "stv1pk",
0x02, "stv1rms",
0x04, "itv1rms",
0x06, "ian1pk",
0x08, "ian1rms",
0x0A, "i1pk",
0x0C, "i1rms",
0x0E, "vccoff",
0x10, "vccon",
0x12, "rearme",
0x14, "magneto",
0x16, "offmanual",
0x18, "interno",
0x1A, "remotein"
};

int t;
char *v;

for (t=0; t <14; t++) cadena[t] = t;

if (cadena[t] == r1tipus *v =cadena[2])

return(v);
}

"Yodai" <yo***@spamnot.mail.vu> escribió en el mensaje
news:VM***********************@telenews.teleline.e s...
Hi all... I am trying to construct an 2x14 array that compares a given
character with the 1st column of data type "t" and returns the second
column's appropriate value "v". I've been trying this, but doesn't seem to
work.... Any idea of what I am missing?

void Detect_type(void)
{
volatile char r1tipus = *R1TIPUS;
unsigned char cadena [14] [2] = {

0x00 , stv1pk,
0x02, stv1rms,
0x04, itv1rms,
0x06, ian1pk,
0x08, ian1rms,
0x0A, i1pk,
0x0C, i1rms,
0x0E, vccoff,
0x10, vccon,
0x12, rearme,
0x14, magneto,
0x16, offmanual,
0x18, interno,
0x1A, remotein
};

int t;
char *v;

for (t=0; t <14; t++) cadena[t] = t;

if (cadena[t] == r1tipus *v =cadena[2])

return(v);
}
Cheers!!

Yodai

Nov 14 '05 #2

P: n/a
Yodai wrote:

Hummm... I have just realized there's an error,
so I correct myself: (still
doesn't work, though) the 2nd column should be as
"stv1pk" to be a character string:

void Detect_type(void)
{
volatile char r1tipus = *R1TIPUS;
unsigned char cadena [14] [2] = {

0x00 , "stv1pk",
0x02, "stv1rms",
0x04, "itv1rms",
0x06, "ian1pk",
0x08, "ian1rms",
0x0A, "i1pk",
0x0C, "i1rms",
0x0E, "vccoff",
0x10, "vccon",
0x12, "rearme",
0x14, "magneto",
0x16, "offmanual",
0x18, "interno",
0x1A, "remotein"
};

struct {
unsigned number;
char *string;
} cadena [] = {
{0x00 , "stv1pk"},
{0x02, "stv1rms"},
{0x04, "itv1rms"},
{0x06, "ian1pk"},
{0x08, "ian1rms"},
{0x0A, "i1pk"},
{0x0C, "i1rms"},
{0x0E, "vccoff"},
{0x10, "vccon"},
{0x12, "rearme"},
{0x14, "magneto"},
{0x16, "offmanual"},
{0x18, "interno"},
{0x1A, "remotein"},
};

--
pete
Nov 14 '05 #3

P: n/a
"Yodai" <yo***@spamnot.mail.vu> wrote:
Hummm... I have just realized there's an error, so I correct myself: (still
doesn't work, though) the 2nd column should be as "stv1pk" to be a character
string:

void Detect_type(void)
{
volatile char r1tipus = *R1TIPUS;
May I ask why this char is volatile?
unsigned char cadena [14] [2] = {

0x00 , "stv1pk",
0x02, "stv1rms",
Your first item in each row is a char; the second is a pointer to char
(which points to the string literal in question). Arrays do not work
that way. Arrays are meant for a collection of data of the same type.

What you're looking for is a struct. Structs were created to hold data
of different types. For example, you could declare a

struct entry {
char index;
char *data;
};

and then you could have

struct entry cadena[14] = { ... };
Note, BTW, that it is unwise to have several instances of this magic
number 14 in your code. Sooner or later, you'll need to change it, and
then you might forget one. Use a #defined constant, or make this
definition

struct entry cadena[] = { ... };

(which will ask your compiler to figure out the size of the array
itself; this only works with initialised arrays, of course), and then
later on use (sizeof cadena/sizeof *cadena) instead of the magic 14.
int t;
char *v;

for (t=0; t <14; t++) cadena[t] = t;
This obviously does not work, since cadena[t] is a char[2], while t is
an int.
Besides, if all indices are equal to their position in the array, why do
you need the index at all?
if (cadena[t] == r1tipus *v =cadena[2])


Neither does this, for much the same reasons. Look up how arrays and
structs work _exactly_ in your C textbook; you can't just throw in a
random index wherever you like.

Richard
Nov 14 '05 #4

P: n/a
//Ok..... I see. I've been reading in my C manual and your answers and I
still can't figgure out if struct is really what I need. The thing is:

//I have a variable which sends me a char for 0x00 to 0x1A and I have to
return the value that corresponds by comparing "type" to
//the first column of my struct. Let's see if I got it right:
void Detect_type(void)
{
volatile char type = *TYPE
char *value;
int i;

struct decide {
unsigned num;
char *string;
}cadena [] = {
{0x00 , "stv1pk"},
{0x02, "stv1rms"},
{0x04, "itv1rms"},
{0x06, "ian1pk"},
{0x08, "ian1rms"},
{0x0A, "i1pk"},
{0x0C, "i1rms"},
{0x0E, "vccoff"},
{0x10, "vccon"},
{0x12, "rearme"},
{0x14, "magneto"},
{0x16, "offmanual"},
{0x18, "interno"},
{0x1A, "remotein"},
};

for(i=0,i<14,i++)
{
if (cadena.num[i] == type) value = cadena.string[i] ;
else value = "empty" ;
}

return(value);

}

Any good?? well it doesn't compile, but I think it gives an Idea of what I
need to do.... Any light upon my ignorance? :)

Cheers!

Yodai

struct {
unsigned number;
char *string;
} cadena [] = {
{0x00 , "stv1pk"},
{0x02, "stv1rms"},
{0x04, "itv1rms"},
{0x06, "ian1pk"},
{0x08, "ian1rms"},
{0x0A, "i1pk"},
{0x0C, "i1rms"},
{0x0E, "vccoff"},
{0x10, "vccon"},
{0x12, "rearme"},
{0x14, "magneto"},
{0x16, "offmanual"},
{0x18, "interno"},
{0x1A, "remotein"},
};

--
pete

Nov 14 '05 #5

P: n/a
Yodai wrote:
//Ok..... I see. I've been reading in my C manual and your answers and I
still can't figgure out if struct is really what I need. The thing is:

//I have a variable which sends me a char for 0x00 to 0x1A and I have to
return the value that corresponds by comparing "type" to
//the first column of my struct. Let's see if I got it right:
void Detect_type(void)
{
volatile char type = *TYPE
char *value;
int i;

struct decide {
unsigned num;
char *string; const char *string;
}cadena [] = {
{0x00 , "stv1pk"},
{0x02, "stv1rms"},
{0x04, "itv1rms"},
{0x06, "ian1pk"},
{0x08, "ian1rms"},
{0x0A, "i1pk"},
{0x0C, "i1rms"},
{0x0E, "vccoff"},
{0x10, "vccon"},
{0x12, "rearme"},
{0x14, "magneto"},
{0x16, "offmanual"},
{0x18, "interno"},
{0x1A, "remotein"},
};

for(i=0,i<14,i++)
{
if (cadena.num[i] == type) value = cadena.string[i] ;
else value = "empty" ;
}

return(value); Detect_type is void, you cannot return a value from such a function.
Make it const char* Detect_type(void).


}

for(i = 0; i < sizeof(cadena) / sizeof(cadena[0]); i++)
if(cadena[i].type == type)
return cadena[i].string;

return "empty";

HTH
Bjørn

--
The worlds fastest web server is now available
at http://highlander.metasystems.no:2000. Enjoy!
Nov 14 '05 #6

P: n/a


Yodai wrote:
//Ok..... I see. I've been reading in my C manual and your answers and I
still can't figgure out if struct is really what I need. The thing is:

//I have a variable which sends me a char for 0x00 to 0x1A and I have to
return the value that corresponds by comparing "type" to
//the first column of my struct. Let's see if I got it right:
void Detect_type(void) //return type should be char * since you are returning value
char * Detect_type(void) {
volatile char type = *TYPE
char *value;
int i;

struct decide {
unsigned num;
char *string;
}cadena [] = {
{0x00 , "stv1pk"},
{0x02, "stv1rms"},
{0x04, "itv1rms"},
{0x06, "ian1pk"},
{0x08, "ian1rms"},
{0x0A, "i1pk"},
{0x0C, "i1rms"},
{0x0E, "vccoff"},
{0x10, "vccon"},
{0x12, "rearme"},
{0x14, "magneto"},
{0x16, "offmanual"},
{0x18, "interno"},
{0x1A, "remotein"},
};

for(i=0,i<14,i++) for(i=0;i<14;i++) {
if (cadena.num[i] == type) value = cadena.string[i] ; if (cadena[i].num == type) {value=cadena[i].string; break;}
//cadena is an array,cadena[i] is a structure variable
//of which num is num and string are members. else value = "empty" ;
}

return(value);

}

[snip]

the break I added is most probably needed by your logic, otherwise
value would always be assigned with empty (if type is anything other
than 0x1A)

--
Shanmu.

Nov 14 '05 #7

P: n/a
"Yodai" <yo***@spamnot.mail.vu> wrote in message
news:%c***********************@telenews.teleline.e s...
//I have a variable which sends me a char for 0x00 to 0x1A and I have to return the value that corresponds by comparing "type" to
//the first column of my struct. Let's see if I got it right:
void Detect_type(void)
You seem to want the function to return a string, but you have declared it
to return nothing. Try:

const char *Detect_type(void);
{
volatile char type = *TYPE
Missing semicolon. I don't see the use of making type volatile.
char *value;
int i;

struct decide {
unsigned num;
char *string;
}cadena [] = {
{0x00 , "stv1pk"},
{0x02, "stv1rms"}, [snip] };

for(i=0,i<14,i++)
You want semicolons not commas to seperate the parts of the for loop.
{
if (cadena.num[i] == type) value = cadena.string[i] ;
else value = "empty" ;
}

return(value);

When you find the matching type, you should stop looking. Otherwise, if the
loop does not terminate (because the matching type is the last one in the
list), the next and any subsequent iterations will assign "empty" to value.

Instead, try:

value = "empty";
for (i = 0; i < (sizeof cadena / sizeof *cadena); i++)
{
if (cadena.num[i] == type) value = cadena.string[i];
}
return value;

Alternatively, eliminate value altogether:

for (i = 0; i < (sizeof cadena / sizeof *cadena); i++)
{
if (cadena.num[i] == type) return cadena.string[i];
}
return "empty";
}


Instead of a struct and loop, you could use a lookup table:

const char *
Detect_type(void)
{
static const char *strings[] =
{
/* 0x00 */ "stv1pk",
/* 0x01 */ 0, /* NULL pointer used for invalid types */
/* 0x02 */ "stv1rms"
/* .... */
};

char type = *TYPE;

if (type < 0 || type >= (sizeof strings / sizeof *strings)
|| !strings[type])
{
return "empty";
}
return strings[type];
}

Alex
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.