473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

trying to construct an array.....

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
7 1647
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
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
"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
//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
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


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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Oli | last post by:
Nooby question but when I try and get this to work (it should identify which rows in the database have GK, MID, DEF, FWD against them and then put SELECTED into the option value to give a default...
2
by: phpfrizzle | last post by:
Hi there, this might sound strange, but i need to construct a name of a variable: i have these vars (and loads more): $menu_ho = massageMe("$string_1"); $menu_do = massageMe("$string_2");...
12
by: Don Bruder | last post by:
A week or two ago, I asked here about porting Python to C. Got some good answers (Note 1) but now I've got another question. Actually, more a request for clarification of a topic that both the...
6
by: Lasse Skyum | last post by:
I'm currently learning STL and I hate not knowing what is gooing on "inside" STL... not because I really _need_ to know it to develop my game project, but that's just my nature... like most of you...
22
by: MLH | last post by:
I would like to test some of this code in the debug window... Option Compare Database Option Explicit Private Sub Command0_Click() #If Win32 Then MsgBox "Hey! It's WIN32." #End If End Sub
4
by: golubovsky | last post by:
Hi, Is there an easy way to construct a function call out of a function and an array of actual arguments? e. g. given a function `fun' and an array I need to obtain equivalent of...
9
by: =?Utf-8?B?ZGg=?= | last post by:
If there's bunch of hex numbers: 0xA6, 0xD9, 0x00, 0xAA, 0x00, and 0x62, how to construct a string ("string" in C#) with those hex numbers? Thanks!
5
by: not_a_commie | last post by:
It seems that the only way to construct a struct from a type is to use Activator.CreateInstance. Is that true? Can anyone improve (performance-wise) upon this function below: /// <summary>...
3
by: =?GB2312?B?0rvK18qr?= | last post by:
Hi all, Recently I asked a question on this group: I got these suggestion: 1) try construct
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.