473,396 Members | 2,020 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,396 software developers and data experts.

Beginner array question

In the following code, can someone please explain to me what exactly
is happenening as far as accessing a 2-dimensional array with a 1-
dimensional index? What is the benifit of this?

Thanks in advance!
ck_name (name_list, max_num, item, index)

char item[];
char name_list[MXMARK][MXNAME]; /* MXMARK=80, MXNAME=24 */
short max_num;
short *index;

{ /* ck_name.c */

int np;

for (*index = 0; *index < max_num; (*index)++)
{
if (strcmp (name_list[*index], item) == 0)
return (0);
}
return (1);

} /* ck_name.c */
Jan 11 '08 #1
2 1278
In article <54**********************************@v29g2000hsf. googlegroups.com>,
Stang1 <sc***********@yahoo.comwrote:
>In the following code, can someone please explain to me what exactly
is happenening as far as accessing a 2-dimensional array with a 1-
dimensional index? What is the benifit of this?
> char name_list[MXMARK][MXNAME]; /* MXMARK=80, MXNAME=24 */
> if (strcmp (name_list[*index], item) == 0)
Hint: strcmp is expecting the *address* of a string.
name_list[*index] is not addressing an individual 'char'.
--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes
Jan 11 '08 #2
"Stang1" <sc***********@yahoo.comwrote in message
>
In the following code, can someone please explain to me what exactly
is happenening as far as accessing a 2-dimensional array with a 1-
dimensional index? What is the benifit of this?

Thanks in advance!

The important thing to remember is that in C two-dimensional arrays and
higher are quite advanced. The basic idea is simple enough, the syntax for
accessing rows or other elements gets very difficult very quickly.
ck_name (name_list, max_num, item, index)

char item[];
char name_list[MXMARK][MXNAME]; /* MXMARK=80, MXNAME=24 */
In this case you have declared a contiguous block of memory, 80 * 24 chars
wide, or an array of 80 arrays of 24 chars.
short max_num;
short *index;

{ /* ck_name.c */

int np;

for (*index = 0; *index < max_num; (*index)++)
{
if (strcmp (name_list[*index], item) == 0)
return (0);
}
return (1);

} /* ck_name.c */
Now we are hittling another complexity, which is array-pointer equivalence.
In most situations, the name of an array is the same thign as a pointer to
the first element of that array. so

name_list[12]

is the same as a pointer to the first char in the thirteenth na,e in your
list. You can then manipulate it as you would any other string, for instance
by passing it to strcmp(). If you try to write beyond element 23 you will
corrupt the next name in the list.

In real programs you only rarely want a hard-coded limit of 80 names. So you
would calculate the number of names needed and then call malloc(). However
that's a whole new topic.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jan 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jvax | last post by:
Hi all, I hope I'm posting in the right NG... I have a data text file I want to read from a c++ program. the data file goes like this: 90 # number of balls 33 42 13
44
by: lester | last post by:
a pre-beginner's question: what is the pros and cons of .net, compared to ++ I am wondering what can I get if I continue to learn C# after I have learned C --> C++ --> C# ?? I think there...
5
by: Tarjei Romtveit | last post by:
I'm still a newbie into C++ programming, so I got a quite foolish string related question. Using: Dev-cpp 4.9.9.2 (I think Dev-Cpp uses a gcc compiler of some sort) If i declare a char...
4
by: anonymous | last post by:
Hi Folks, I have a form with two Dropdown list boxes, which get loaded with data from Database. DropDownList1 gets data from Table1 and DropDownList2 gets data from Table2 Table1 has a...
7
by: smartbeginner | last post by:
I got from a book a 2D array(a is resolved by compiler as a=*(&a+total_columns*i+j); // ->1 And I know when I use pointer to array I should refer a=*(*(a+i)+j); ->2 Is this also resolved...
9
by: me | last post by:
Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am...
13
by: sathyashrayan | last post by:
Dear group, pls go through the following function definition: function at_show_aux(parent, child) { var p = document.getElementById(parent); var c = document.getElementById(child); var top ...
2
by: OrdinarySpore | last post by:
Hi. I am having a problem with a simple IF statment on this Javascript. I found this script on scriptsource and want to see if I can add an IF statment to display a "congradulations" line of text...
10
by: Roman Zeilinger | last post by:
Hi I have a beginner question concerning fscanf. First I had a text file which just contained some hex numbers: 0C100012 0C100012 ....
1
by: Arjun234 | last post by:
hi, I have a program to calculate the distance. its like this: open(IN, "/path/outModified.pl") or die "$!"; while (my $line = <IN>) { chomp($line); my @array = (split (/\s+/, $line)); #...
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: 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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.