473,388 Members | 1,220 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,388 software developers and data experts.

Reducing Repeated code for arrays

Hey there, having a bit of a problem trying to acheive something..

Ive got 2 arrays, ArrayA, ArrayB both are of type char with [5][7]
dimensions.

Ive got code in a function, which does
if(ArrayA[x][y] == 'A') { // do something }

but i also have
if(ArrayB[x][y] == 'A') { // do something }

so for each array im doubling it.

Im wondering, if i could do, if(SOMETHING[x][y] ==

and then reference the arrayname in SOMETHING

Any ideas please?

Thanks
Nov 14 '05 #1
2 1006
In article <J4qlc.26$XC3.15@newsfe6-win>, JellyBum wrote:
Ive got 2 arrays, ArrayA, ArrayB both are of type char with [5][7]
dimensions.

Im wondering, if i could do, if(SOMETHING[x][y] ==
and then reference the arrayname in SOMETHING


You can access both arrays via a pointer. Your pointer must have the
type:
char (*pArray)[7]

if you want to be able to point it to arrays of type:

char ArrayA[5][7]

then you can say:

pArray = ArrayA; // or: pArray = ArrayB
if (pArray[x][y] == ...

Regards,
--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
2614 JE Delft, The Netherlands
+31 (0)6 155 244 60
Nov 14 '05 #2

"JellyBum" <......@......com> wrote in message
news:J4qlc.26$XC3.15@newsfe6-win...
Hey there, having a bit of a problem trying to acheive something..

Ive got 2 arrays, ArrayA, ArrayB both are of type char with [5][7]
dimensions.

Ive got code in a function, which does
if(ArrayA[x][y] == 'A') { // do something }

but i also have
if(ArrayB[x][y] == 'A') { // do something }

so for each array im doubling it.

Im wondering, if i could do, if(SOMETHING[x][y] ==

and then reference the arrayname in SOMETHING

Any ideas please?


Use a function.

#include <stdio.h>
#include <stdlib.h>

void foo(char (*arr)[7])
{
size_t row = 0;
size_t col = 0;
char value = 'A';

printf("[%ld][%ld]", (unsigned long)row, (unsigned long)col);

if(arr[0][0] == value)
printf(" == ");
else
printf(" != ");

printf("'%c'\n", value);
}

int main()
{
char ArrayA[5][7] = {'A'};
char ArrayB[5][7] = {'B'};

printf("ArrayA");
foo(ArrayA);

printf("ArrayB");
foo(ArrayB);

return 0;
}
-Mike
Nov 14 '05 #3

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

Similar topics

7
by: Peter Salzman | last post by:
Hi all, Newish PHP programmer here. I wrote a form select class to help reduce code clutter, but I don't think the effort was worth it. I was hoping to post my attempt and get some ideas from...
15
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html...
5
by: Salvador I. Ducros | last post by:
Greetings all, I was hoping someone might be able to point me in the right direction. I'm currently using std::vector to maintain several lists whose objects are of unrelated types (classes)....
0
by: Filipe Martins | last post by:
Hello to all. Ever had a problem when your print some reports in a printer other the one you use more frequently, in which the report width span more tban one page? I did, and didn't liked it. ...
1
by: jez123456 | last post by:
Hi, I have the following method which contains some repeated code. Could this be reduced somehow? Thanks. private void chooseDatabase() { string dbFile = GetPath(); string systemDB =...
3
by: IraqiAli | last post by:
ok i have a couple of textboxes. The user is required to input values from 1 to 6 into those boxes. and submit it, those values would then go into an array. What i need to do is do checks to make...
7
by: bagelman | last post by:
Hi, I have a string Array. Its length is 100. I want to search the array to find repeated strings in it. And after finding repeated strings I want to write to screen which word repeated how many...
4
by: d0ugg | last post by:
Hello everyone, I'm creating a program that it is suppose to add, subtract, multiply and also divide fractions and after that, the result has to be reduced to the lowest terms. However, I'm not...
9
by: C#_Help_needed | last post by:
I need help with the following question. THANKS :) Write a program in c# that takes in a directory as a command line parameter, and returns the longest repeated phrase in ALL text files in that...
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:
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,...

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.