473,796 Members | 2,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(voi d)
{
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 1676
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(voi d)
{
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.telel ine.es...
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(voi d)
{
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(voi d)
{
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(voi d)
{
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(voi d)
{
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(voi d)
{
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(voi d).


}

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(voi d) //return type should be char * since you are returning value
char * Detect_type(voi d) {
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.telel ine.es...
//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(voi d)
You seem to want the function to return a string, but you have declared it
to return nothing. Try:

const char *Detect_type(vo id);
{
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(voi d)
{
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
2062
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 of what the entry is currently) it just adds SELECTED to all the results as if $row is returning true against all four conditions? What am I doing wrong? Thanks, O.
2
1516
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"); $menu_co = massageMe("$string_3"); they stand for some massaged strings, which are displayed as a body title.
12
2855
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 Python tutorial and docs leave a touch murky to my understanding. Dictionaries/"dict" types... Am I understanding/interpreting correctly when I go with the idea that a "dict" variable can be looked at as (effectively) two parallel arrays?...
6
4656
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 at this grounp I suspect. So the question is: How does a std::vector construct and destruct the elements in it? I know it has something to do with an allocator... Suppose I wrote:
22
6324
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
1990
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 `fun(1,2,3)' `fun' is not a method of any object, just a function. Can `apply' be
9
4524
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
3272
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> /// Create an object of the given type /// A default constructor is required to be successful. /// A failure will return null. /// </summary>
3
2550
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
9679
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9527
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.