Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2d Array Help

Status
Not open for further replies.

sleuth

Programmer
Jan 12, 2001
134
US

Hi,

I've almost worked through my problem previously posted, now I'm having trouble with a 2d array.

char table[10][50];

table[1234567]['A']='Test';

I get a segmentation fault for that. The number is an id number, I have no need to have it positioned in that spot of the array.

So how can I do this, What I need to do with it once it works is get to the A through the id number.

table[1234567]['A']

Something like that.

Thanks,
Tony
 
Tony,

Here is some code I pasted in from my 2D array. The first 1D array is an index into the 2D array. If we have a match in the loop, I print a manufacturing location. It's probably not what you are looking for, or even the best solution but it's what I have. I had to paste in fake manufacturing locations due to trade secrets. In this case the bios info would be like "P12345678" with the "P" representing a manufacturing location. With that in mind the loop would print our "PLocation"

Tim

unsigned char *ManChar;
ManChar = (bios + 0x281); /* pointer to a string in memory */

const char manLookup[20]={0x41,0x42,0x43,0x44,0x46,0x47,0x48,0x4A,0x4B,0x4C,0x4D,0x4E,0x50,0x51,0x52,0x53,0x54,0x55,0x58,0x00 };

const char manString[20][25]={{"ALocation"},{"BLocation"},{"CLocation"},{"DLocation"},{"ELocation"},{"FLocation"}, {"GLocation"},{"HLocation"},{"ILocation"},{"JLocation"},{"KLocation"},{"LLocation"},{"MLocation"},{"NLocation"},{"OLocation"},{"PLocation"},{"QLocation"},{"RLocation"},{"SLocation"} };


for( i=0; i<sizeof(manLookup); i++)
{
if (manLookup == *ManChar)
{
printf(&quot;\tManufactured At : %s\n&quot;,manString);
found = 1;
break;
}
}
 

I'm still having trouble,

I'm always thinking in terms of how I would do it in perl, this is where I think the larger part of the problem is.

Would you advise me on how you would access the value of a Letter, from a ten digit number.

Or just explain why,

const char manString[20][25]={{&quot;ALocation&quot;},{&quot;BLocation&quot;},{&quot;CLocation&quot;},{&quot;DLocation&quot;},{&quot;ELocation&quot;},{&quot;FLocation&quot;}, {&quot;GLocation&quot;},{&quot;HLocation&quot;},{&quot;ILocation&quot;},{&quot;JLocation&quot;},{&quot;KLocation&quot;},{&quot;LLocation&quot;},{&quot;MLocation&quot;},{&quot;NLocation&quot;},{&quot;OLocation&quot;},{&quot;PLocation&quot;},{&quot;QLocation&quot;},{&quot;RLocation&quot;},{&quot;SLocation&quot;} };

Doesn't let me access it using manString{&quot;ALocation&quot;} or manString[&quot;ALocation&quot;].

That's perl thinking right there, &quot;Always looking for the shortcut&quot;.

Good thing you didn't reveal any trade secrets, I wouldn't want you to have to kill me, hehe.

Thanks,
Tony
 
Hi,
What you need is not a 2d array. In C a string is already an array of char's.

A structure containing an array of char's and a char would work.

Below is an example.
// Start Code
#include <stdio.h>
#include <string.h>

struct table
{
char idnum[8];
char idlet;

};

int main()
{
struct table list[50];

strcpy(list[0].idnum, &quot;1234567&quot;);
list[0].idlet = 'A';
return 0;
}

// End Code
The above gives a list of 50 ID numbers and letters.
Hope this helps.
Pappy
You learn something everyday.
 
What you have done:
char table[10][50]; created 2d array of chars.
This array can hold 10 string with 49 chars in each.

By typing this:
table[1234567]['A']='Test';
you are trying to access char in 1234567 string (but you have only 10), on position (int)A. C will treat 'A' as in integer and it will be something around 100.
Anyway just read C book you are so far from C basic that I don't see the point to go any further.
 

Ok Folks, Thank you, I'm going to try what you said Pappy, Thanks for the explanation Lim.

I suppose I'm just be getting creative now.

Tony
 
Tony, I think Pappy set you straight. Use a structure. To access ALocation in my example would be
manString[0], or if you want SLocation it is
manString[18]. I search the 1D array for the first letter of my string. I use the location in the 1D array as an index into the 2D array. Like I said, it ain't the best way to do what I am doing. If I had to re-do it, I would use a structure. Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top