I want to compare two arrays of different sizes in a particular function. I wrote a function like this:
static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])
{
static int index=0;
static bool isequal = false;
for(int i = 0 ; i<count_current; i++ )
{
for(int j = 0; j<6 ; j++ )
{
if (array_1[j] == array_2[i][j])
{
index++ ;
}
else
{
index=0;
return false;
}
}
if (index == 6)
{
index=0;
isequal = true;
i = count_current + 2;
}
else
{
index=0;
isequal = false;
}
}
return isequal;
}
Define some variables;
static bool matching_array;
static uint8_t one_dimensional_array[6];
static uint8_t two_dimensional_array[10][6];
Then I use the function like this;
matching_array= compare_arrays( one_dimensional_array, two_dimensional_array);
But I got an error like this;
..\..\..\main.c(857): error: #98: an array may not have elements of this type
Do you have any suggestion ?
static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][])
The second dimension must be present - otherwise compiler can't decide how much position to stride through when someone writes like array_2[y][x].
static bool compare_arrays(uint8_t array_1[] , uint8_t array_2[][6])
Actually 2d array decays into pointer to it's first element - which is of type uint8_t (*)[6]. And the single dim array will similarly decay here into uint8_t*. Remember that the first dimension of passed array is not considered by compiler.Arrays declarations must have all, except the first, sizes defined.
Related
I want to copy an 2D array to a new 2D array within an internal buffer.
Let's suppose I have the following function:
uint8 the_2D_array[100][7];
void Get_2D_Array(uint8 **array)
{
*array = &the_2D_array[0][0]; // Function which returns pointer to the first element of the 2D array
}
Later in my code I'm expecting to do something like this:
myBuffered_Aray[100][7];
uint8 *pValues;
Get_2D_Array(&pValues)
{
for (uint8 i = 0; i < 100; i++)
{
for (uint8 j = 0; j < 7; j++)
{
(void)memcpy(myBuffered_Aray[i][j], (u8_t*)&pValues[i][j], sizeof(uint8));
}
}
}
The part with
(u8_t*)&pValues[i][j]
will not work because of "Expression must have pointer to object type".
How to do it correctly?
Below, I've defined a Map struct in C. It functions as a map, with setValue and getValue functions. Key values default to -1.
typedef struct {
int key;
int value;
} Index;
typedef Index Map[1000];
void initMap(Map *map)
{
for (int i = 0; i < 1000; i++)
{
Index thisIndex = *map[i];
thisIndex.key = -1;
thisIndex.value = 0;
}
}
int getValue(Map *map, int keyToGet)
{
for (int i = 0; i < 1000; i++)
{
Index thisIndex = *map[i];
if (thisIndex.key == keyToGet)
{
return thisIndex.value;
break;
}
}
return -1;
}
void setValue(Map *map, int keyToSet, int valueToSet)
{
int set = 0;
for (int i = 0; i < 1000; i++)
{
Index thisIndex = *map[i];
if (thisIndex.key == keyToSet)
{
thisIndex.value = valueToSet;
set = 1;
break;
}
}
if (set == 1)
return;
for (int i = 0; i < 1000; i++)
{
Index thisIndex = *map[i];
if (thisIndex.key == -1)
{
thisIndex.key = keyToSet;
thisIndex.value = valueToSet;
break;
}
}
}
int findValue(Map *map, int valueToGet)
{
for (int i = 0; i < 1000; i++)
{
Index thisIndex = *map[i];
if (thisIndex.value == valueToGet)
return thisIndex.key;
}
return -1;
}
At some point in the code, when run, I get a SegFault, presumably for accessing or trying to write over memory over which I do not have jurisdiction. My question is, where is that happening? Which function could be doing that, and where? I've sifted through multiple times and I can't seem to find where that would be happening.
There are two major errors in your code.
First, you typedef Map to an array. That means when you have a function prototype like this:
int getValue(Map *map, int keyToGet);
you really get something like this:
int getValue(Index (*map)[1000], int keyToGet);
In C, definition mimicks use, so you should access your map elements like this:
Index thisIndex = (*map)[i];
Your way to use it, *map[i] is equivalent to *(map[i]) and requires the array to be an array of 1,000 pointers to Index, which it isn't.
The (*map)[i]sytnax complicated, and you don't need it. Because your Map is an array, it decays into a pointer to its first element. You do not need to pass a pointer to an array if you want to modify the elements. It is enough to pass a pointer to the first element, which in your case can be either of:
int getValue(Map map, int keyToGet);
int getValue(Index map[], int keyToGet);
int getValue(Index *map, int keyToGet);
Accessing the elements of the array is then just map[i].
Fixing that fixes your segmentation fault, but it doesn't fix the fact that your map doesn't work. When you assign to a struct like this:
Index thisIndex = map[i];
and then modify thisIndex, you will not modify anything in your map! Here, thisIndex is a copy. When thisIndex goes out of scope, all modifications are lost.
You can either use the map directly:
if (map[i].key == keyToSet) {
map[i].value = valueToSet;
return;
}
(and why don't you? thisIndex is longer than map[i]), or you can make thisIndex a pointer to the array element:
Index *thisIndex = &map[i];
if (thisIndex->key == keyToSet) {
thisIndex->value = valueToSet;
return;
}
Here, you access and modify the fields of map[i] through the pointer.
Hiding an array in a typedef is probably not such a good idea. Perhaps at one time you want to keep the number of active items alongside the array. (That would make your code more efficient, because you wouldn't have to traverse all 1,000 items in the worst case.) In that case, you could make Map a struct with an array of 1000 key/value pairs and a count. Then you would have to pass pointers to the struct type, so that you can update the fields. This is left as an exercise ... :)
so I'm really new at this, and I was wondering how I would go about passing a local variable in a function (in terms of the initialization). For example, I have one function (move) that declares and initializes two variables (t_row and t_column) to be used in a for loop, and within that loop, I have another functions (swap), that is called if certain conditions are met. How do I go about using those variables in the swap function. I know I need to declare them, but their initialization in the swap function depends on what iteration of the for loop swap was called. Thanks in advance for any help!
bool move(int tile)
{
for (int t_row = 0; t_row < d; t_row++)
{
for (int t_column = 0; t_column < d; t_column++)
{
if (tile == board[t_row][t_column])
{
if (0 < t_row && board[t_row - 1][t_column] == 0)
{
swap(t_row - 1, t_column);
return true;
}
}
}
}
return false;
}
void swap(int row_new, int column_new)
{
int t_row;
int t_column;
int hold = board[t_row][t_column];
board[t_row][t_column] = 0;
board[row_new][column_new] = hold;
}
The easiest way I can see to do this would be to pass in the values of the old row and column.
void swap(int row_new, int col_new, int row_old, int col_old) {
int hold = board[row_old][col_old];
board[row_old][column_old] = 0;
board[row_new][column_new] = hold;
}
I'm trying to compare a string using a pointer to the array and the destination as defined.
string destination;
int flightcompare(Flights FDA[], String destination)
{
int j=0;
Flights founddestination[10];
for (int i=0;i<MAXARRAYSIZE;i++)
{
(strcmp(Flight *FDA[i]->destination,destination)==0);
founddestination[j]= FDA[i];
j++;
}
return 1;
}
I am not sure about the programming language but I am assuming it is one with stringas a data type.
In your code, there's a Semicolon at the end of
strcmp(Flight *FDA[i]->destination,destination)==0);
which makes use of strcmpredundant.
Remove that Semicolon.
Plus You don't need to pass Flight*to strcmp
So, With these modifications, Function should look like:
int flightcompare(Flights FDA[], String destination)
{
int j=0;
Flights founddestination[10];
for (int i=0;i<MAXARRAYSIZE;i++)
{
if(strcmp(FDA[i]->destination,destination)==0)
{
founddestination[j]= FDA[i];
j++;
if(j >= 10)
{
break; // Stop Looping as Array is full
}
}
}
return j; // Return Count of the Flights found.
}
your strcmp line doesn't make any sense, cause you're not checking the boolean value which is the result of your comparison.
In general, it should be put in an if statement.
There's another problem though since there's no need to compare string objects with strcmp.
You can just compare them with operator ==.
if (FDA[i].destination == destination) {
// they're equal -> do something
} else {
// they're not equal -> do something else
}
That's assuming Flights type has a 'destination' public member.
Moreover, why put them in the founddestination array if you're not using it? and what is the purpose of returning an int?
If you want to know if there's any mismatching destination you can return a boolean.
If you want to return the number of equal destinations / non equal destinations you can just count them in an int.
Assuming you aim for the boolean solution i'd write:
bool flightcompare(Flights FDA[], String destination) {
for (int i = 0; i < MAXARRAYSIZE; ++i) {
if (FDA[i].destination != destination) {
return false;
}
}
return true;
}
If you want to return the amount of matching flights i'd write:
int flightcompare(Flights FDA[], String destination) {
int count = 0;
for (int i = 0; i < MAXARRAYSIZE; ++i) {
if (FDA[i].destination == destination) {
++count;
}
}
return count;
}
In my app I have 2 images with same dimensions,that I would to give their RGB data and compare them.In j2me we can not use java.util.Arrays and so Arrays.equals(array1, array2) method. One way to compare them is using for loop and compare each element of two int array,but i'm looking for better way.When I search in web I found ArrayUtils class,here,that has some equals() methods,but it's method compare two arrays of objects and before compare int arrays convert them to Enumeration by arrayToEnumeration(Object array) that creates an enumeration from given object.
Finally this is my question:
Is there a better way to compare two int arrays in j2me?
Try something like this.. From the Util class
public static boolean equals(byte[] a, byte[] a2) {
if (a==a2)
return true;
if (a==null || a2==null)
return false;
int length = a.length;
if (a2.length != length)
return false;
for (int i=0; i<length; i++)
if (a[i] != a2[i])
return false;
return true;
}
private int compareArray(String[] _array1, String[] _array2){
Vector m_length1 = new Vector();
Vector m_length2 = new Vector();
if(_array1 && _array2!=null)
{
for(int i=0; i<_array1.length; i++){
if(m_length1[i]!=null){
m_length1.add(_array1[i]);
}
}
for(int j=0; j<_array2.length; j++){
if(m_length2[j]!=null){
m_length2.add(_array2[j]);
}
}
}
if(m_length1.size()==m_length2.size()){
return 0; /*matching*/
}else{
return -1; /*no matching*/
}
}
You can modify it as int array or you can compare byte of each value.
For instance;
byte sample[] = {0,0,0,0};
sample= yourValue.getBytes();
Convert byte array to String:
public boolean equals(byte[] b1, byte[] b2){
String strB1 = new String(b1);
String strB2 = new String(b2);
if(strB1.equals(strB2)){
return true;
}
else{
return false;
}
}