How cast array[4] to array[2][2]? - c

I have a array short frame[4] and I want it as a function parameter as short frame[2][2]
How can I cast it? I tried different things (like *(short [2][2])&frame[0]*), but I still get error messages.
Also not working is if I declare the function with void function(short frame[2][2])
and call the function with function(&frame[0]) while frame is a short frame[4];

I don't think it is a good pratice, anyway:
f((short (*)[2])a);

This works here, albeit with a warning.
#include <stdio.h>
void function(short frame[2][2])
{
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
printf("%d ", frame[i][j]);
printf("\n");
}
int main()
{
short frame[4] = { 0, 1, 2, 3 };
function(&frame[0]);
return 0;
}
What error message do you get?

You can't cast between different dimensional arrays.
It wouldn't know which way round you wanted it.
You'll have to write a function.
In pseudocode:
function castArray(short[4] input){
short[2][2] output = new short[2][2];
output[0][0] = input[0];
output[0][1] = input[1];
output[1][0] = input[2];
output[1][1] = input[3];
return output;
}

You achieve it an easy way like this :
Converting 1-D array into 2-D array
short frame[MAX];
short dup_frame[ROW][COL];
int i,j,k;
for(i = 0 ; i < MAX ; i++)
{
j= i / ROW ; // you can do it by i / COL also
k= i % ROW ; // you can do it by i % COL also
dup_frame[j][k] = frame[i];
}

Related

Array type 'float [3]' is not assignable .Copying 2d Array in C

I have the following code below:
typedef struct
{
float K[6][3]; //Kalman gain ([row][column])
} FilterData;
void setFilterData(const FilterData *filterdata)
{
FilterData r;
int i;
int i1;
for (i = 0; i < 3; i++) {
for (i1 = 0; i1 < 6; i1++) {
r.K[i1 + 6 * i] = filterdata->K[i + 3 * i1];
}
}
}
Compiling this code leads to the following error:
setFilterData.c: In function setFilterData :
setFilterData.c:25:23: error: assignment to expression with array type
r.K[i1 + 6 * i] = filterdata->K[i + 3 * i1];
What is the alternative here?
Using memcpy explicitly also did not help.
FilterData r;
int i;
for (i = 0; i < 6; i++) {
memcpy(r.K + (3*i),filterdata->K + i,sizeof(float) );
memcpy(r.K + (3*i + 1),filterdata->K + (i+6),sizeof(float));
memcpy(r.K + (3*i + 2),filterdata->K + (i+12),sizeof(float));
}
Your problem is the FilterData.K is defined as a 2-dimensional array, but setFilterData() is treating it as if it were a 1-dimensional array.
The minimual fix is to say
r.K[i1][i] = filterData.K[i1][i]
(this assumes the transposition in your original code is a bug, not a feature, I'll have to read more about Kalman gain to know)
But there's no reason to double for-loop and assign each member separately. You could
memcpy(&r, filterData, sizeof(r))
or better yet
r = *filterData
r.K is a two-dimensional array. This means that r.K[n] is a float array of length 3. You can't assign a value to an array. For example, ask yourself what
int A[5];
A = 6;
would mean. If you want to assign a few to a specific location inside a two-dimensional (or multi-dimensional) array, you have to specify all of the indices.
for (i = 0; i < 3; i++) {
for (i1 = 0; i1 < 6; i1++) {
r.K[i1][i] = ... // I'm not sure what you actually want here.
}
}

Exception thrown at 0x7C131F4C (ucrtbased.dll) in ICP LAB ASSIGNMENT PROJECT.exe: 0xC0000005

I was trying to print some array but it won't print no matter what.
Which part did I do wrong?
Is it the array?
int main()
{
int i;
char id[3]; ///sample data wanted to print
id[0] = 'id1';
id[1] = 'id2';
id[2] = 'id3';
for (i = 1; i <= 3; ++i)
{
printf("%s", id[i]); ///The error appeared here////
}
}
i starts at 1, and goes to 3:
for (i = 1; i <= 3; ++i)
But you set up your array so that valid indicies are 0, 1, and 2.
3 is not a valid index.
Convention C-loops always look like this:
for(i = 0; i < 3; ++i)
That is, they start at 0 and go while less than the size of the array.
Not less than or equal to. That is your mistake.
Next, each element of the array is a single character.
But you are trying to initialize them with 3-letters, such as: id1.
A single character can hold ONE LETTER ONLY, not a set of 3 letters.
You are trying to print them out using %s; but %s is for strings, not single characters.
Here is a corrected version of your program.
int main()
{
int i;
char* id[3]; // Declare strings, not characters.
id[0] = "id1"; // Initialize each with a string
id[1] = "id2";
id[2] = "id3";
for (i = 0; i < 3; ++i) // Set loop limit correctly.
{
printf("%s\n", id[i]);
}
}
You invoked undefined behavior by passing data having wrong type: %s expects an pointer to a null-terminated string while you passed id[i], whose type is char (expanded to int here).
You can use %c to display the values of implementation-defined values of multi-character character literals.
Also The loop range is wrong as #abelenky says.
#include <stdio.h>
int main()
{
int i;
char id[3]; ///sample data wanted to print
id[0] = 'id1';
id[1] = 'id2';
id[2] = 'id3';
for (i = 0; i < 3; ++i)
{
printf("%c", id[i]);
}
}
Or do you mean this?
#include <stdio.h>
int main()
{
int i;
const char* id[3]; ///sample data wanted to print
id[0] = "id1";
id[1] = "id2";
id[2] = "id3";
for (i = 0; i < 3; ++i)
{
printf("%s\n", id[i]);
}
}

Concatenating uint8_t to a char*

im really new to C and im having a bit of a complication creating a char* from various uint8_t
My idea is to create a char* where in each location I place a number form a matrix
For example if I have a matrix with:
[1][2][3][4]
[5][6][7][8]
[9][0][1][2]
[3][4][5][6]
id like a char* thats "01234567890123456"
what im doing bit its not working is:
char* string = malloc(sizeof(char)*matrix->height*matrix->width);
for (int i = 0; i < matrix->height ; ++i) {
for (int j = 0; j < matrix->width ; ++j) {
string[i*matrix->height+j] = matrix->value[i][j];
}
}
of course its not working but im a bit lost on how to proceed and I cant find more information regarding this problem.
Any help would be nice,
thanks
Since you're trying to print a string, you need the ASCII character for 0. So, simply add '0' to each number, like so
char* string = malloc(sizeof(char)*(matrix->height*matrix->width + 1));
for (int i = 0; i < matrix->height ; ++i) {
for (int j = 0; j < matrix->width ; ++j) {
string[i*matrix->width+j] = matrix->value[i][j] + '0';
}
}
string[matrix->height*matrix->width] = 0; //null terminator
Note however this isn't exactly the most portable solution.
Also, notice that you want to multiply i by the width, because if you didn't have a square matrix your calculation wouldn't work correctly.
It's kind of unnecessary to have sizeof(char), because the size of a char is defined to be 1 regardless of the byte size.

Initialize an "eye" (identity) matrix array in C

int eye[3][3] = {
{ 1,0,0 },
{ 0,1,0 },
{ 0,0,1 }
};
Is there a shorter way to initialize it? It's so regular that there must be a smarter way to initialize it, especially if it's more than 3x3, say 10x10 or more.
In c99 you can write:
int eye[][3] = { [0][0] = 1, [1][1] = 1, [2][2] = 1 };
all other elements are zeroed, moreover the compiler figures out the size of the array for you. Just don't skip the second size (3).
Btw. in your code you don't have to use the double braces, this would be fine too:
int eye[3][3] = {
1,0,0,
0,1,0,
1,0,1,
};
In c99 you can also leave the trailing comma, just for symmetry and future refactorings
Other solutions probably require you to write some code, which may indeed save you some time/space in file. But note that this way you're splitting declaration and "initialization", which in case of e.g. globals can make a difference.
You can use designated initializers:
int eye[3][3] = { [0][0]=1, [1][1]=1, [2][2]=1};
All the other elements will be initialized to 0 as per C standard's guarantee.
You may try the following:
#define SIZE 3
int eye[SIZE][SIZE] = {0};
for (int i = 0; i < SIZE ; ++i)
{
eye[i][i] = 1;
}
If you want to store {{ 1,0,0 }, { 0,1,0 }, ...} this style of values in square matrix means, you can write a simple logic as below.
#define SIZE 3
int eye[SIZE][SIZE] = {0};
int *p = (int *)eye;
for (i = 0; i < (SIZE * SIZE); i = i + (SIZE + 1))
{
p[i] = 1;
}
or
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
if (i == j)
{
eye[i][j] = 1;
}
else
{
eye[i][j] = 0;
}
}
}
Note : Above logic is only for the sample value you have given. So try to find similar logic if your values are having some relation. If not so, then no other way to initialize it directly even if size of matrix is 1000x1000.

is there a function that lets me look at the next char?

is there a function in c that lets me look at the next char in an array? Also where could I find this information on my own, I tried Google and looking for existing threads on this site.
I am trying to pull numbers from a line, and store those numbers. So I want to do something like
if(c = a number and c "next character" is not a number){value is = value*10+c-'0', store number}
If the current character is array[i], the next character is array[i+1].
You could write a method to do this:
char next_char(char *array, int i, int size){
return (++i) < size ? array[i] : '\0';
}
EDIT: After reading your question something like this may be reasonable.
if(isdigit(array[i]) && !isdigit(next_char(array,i,size)){
..
}
A better solution would be a for loop:
int val = 0;
for(i = 0; i < size; i++){
if(isdigit(i)){
val = 10 * val + array[i] - '0';
}else{
// Store the value
val = 0;
}
}
To get a char from a char array, you just use:
array[i]
where is is an index.
If you want to read every char, you can use a loop:
for(size_t i = 0; i < sizeof(array); i++)
{
char cur = array[i];
}
You could also increment a pointer.

Resources