trying to construct an array.....

R

Richard Bos

Yodai said:
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
 
Y

Yodai

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
 
Y

Yodai

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);
}
 
P

pete

Yodai said:
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"},
};
 
Y

Yodai

//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 == type) value = cadena.string ;
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
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Yodai said:
//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 == type) value = cadena.string ;
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.type == type)
return cadena.string;

return "empty";

HTH
Bjørn
 
S

Shanmu

Yodai said:
//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 == type) value = cadena.string ;

if (cadena.num == type) {value=cadena.string; break;}
//cadena is an array,cadena 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)
 
A

Alex

Yodai said:
//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 == type) value = cadena.string ;
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 == type) value = cadena.string;
}
return value;

Alternatively, eliminate value altogether:

for (i = 0; i < (sizeof cadena / sizeof *cadena); i++)
{
if (cadena.num == type) return cadena.string;
}
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,341
Latest member
hanifree

Latest Threads

Top