How to copy and assign an array of arrays? - c

int zero[5][4] = {
{ 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }
};
int m1[5][4] = {
{ 1, 0, 0, 0 }, { 0, 1, 0, 0 }, { 0, 0, 1, 0 }, { 0, 0, 0, 1 }
};
//errors here
m1 = zero;
m1 = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };
m1[0] = { 0, 0, 0, 0 };
Is there no syntax for this? Do I have to use a loop with indexing to achieve this?

In C, arrays are not assignable or can't be on the left side of assignment (=) operator. Use memcpy.
memcpy(m1, zero, sizeof(zero)); // Assuming size of 'm1' is no less than the size of 'zero'
You can use memset to set an array with a constant value like 0 or -1
memset(m1, 0, sizeof(m1));

Related

Set value of a two dimensional array using initializer syntax

I am making an RPG game, and got stuck at creating the map array.
Code:
const int Nt;
int map[MAP_WIDTH][MAP_HEIGHT]; // global variable definition
void init() {
//...
map = malloc(Nt * Nt * sizeof(int)); // without this line the result is the same
map = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{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/main.c:116:11: error: expected expression before '{' token
116 | map = {
|
The statement:
int map[MAP_WIDTH][MAP_HEIGHT];
declares a two-dimensional array at file scope. Objects declared at file scope have static storage duration and are always initialised. In the absence of a definition, they are implicitly initialised to 0.
From the C standard C11 6.7.9/10:
"... If an object that has static or thread storage duration is not
initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or
unsigned) zero;"
Though, it's good practice to explicitly set it to 0.
int map[MAP_WIDTH][MAP_HEIGHT] = { 0 };
The above statement initialises all the elements of map to 0.
You could instead initialise it as:
int map[MAP_WIDTH][MAP_HEIGHT] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
The above code assumes that MAP_WIDTH and MAP_HEIGHT are macros, and map is declared at file scope.
If you wanna have the initialize it, "grid" style, then you have to do it inside the declaration (assuming the HxW are macros or non-const ints):
int map[MAP_WIDTH][MAP_HEIGHT] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};
You don't use list initializers in init() but in declarations. As for memory management, you have to learn the difference between static allocation and dynamic memory allocation. You shouldn't do both and since you mentioned you're making an RPG, you should check other parts of your code where you might have this conflict.
Of course, you can always go down the route of using 2 loops and initialize each member to 0. Or, just do this:
int map[MAP_WIDTH][MAP_HEIGHT] = { 0 }; // notice the single braces

How to change datatype of column in FeatureCollection in GEE

I have FeatureCollection with 4 columns with different datatypes. One of them is "long" which I neet to converte to "Int". The FeatureCollection was imported as shapefiles. I need this column as "Int" to do classification.
type: FeatureCollection
id: users/aleksandramonika/pola_beskidy_trening
version: 1667257388739078
columns: Object (4 properties)
id: Long
class: Long
name: String
system:index: String
I don't know how to access and change type of a column.
I tried to change the value with remap, but the datatype was changed to "any".
FeatureCollection (30 elements, 4 columns)
type: FeatureCollection
columns: Object (4 properties)
id: Long
class:
name: String
system:index: String
var agriculture = ee.FeatureCollection(agriculture).remap(
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'class');

warning: excess elements in array initializer

I'm trying to create 2D array.
int main() {
int stalagmite[10][6] = { { 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
{ 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 },
{ 1, 0, 0, 1, 0, 0, 0, 0, 0, 1 },
{ 1, 1, 0, 1, 1, 1, 0, 1, 0, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 },
{ 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 } };
printf("%d\n", stalagmite[3][3]);
}
Output as follows:
deneme.c:9:51: note: (near initialization for ‘stalagmite[5]’)
deneme.c:9:54: warning: excess elements in array initializer
9 | { 0, 1, 1, 0, 1, 1, 0, 1, 1, 0 } };
Trying to define an array with initializer. When I try to compile it always return a error stated above. I tried to change size, data type but it had no effects.
int stalagmite[10][6] defines an array of 10 arrays of 6 int, whereas your initializer specifies 6 arrays of 10 integers.
The definition should probably be changed to int stalagmite[6][10] = {...} or possibly int stalagmite[][10] = {...}.

I'm trying to mutate bytes coming from a stream because decoding, but it's not working

I'm trying to wrap an io.ReaderCloser, which in production would come from a request handler, with a custom reader that can be passed into a JSON decoder.
I created the below
import (
"io"
)
// RemoveNull is a stream wrapper that should remove null bytes from the byte stream
type RemoveNull struct {
Reader io.ReadCloser
}
// NewRemoveNullStream creates a new RemoveNull reader which passes the stream through a null check first
func NewRemoveNullStream(reader io.ReadCloser) RemoveNull {
return RemoveNull{
Reader: reader,
}
}
// Read wraps a Reader to remove null bytes in the stream
func (null RemoveNull) Read(p []byte) (n int, err error) {
n, err = null.Reader.Read(p)
if err != nil {
return n, err
}
nn := 0
for i := range p {
if p[i] != 0 {
p[nn] = p[i]
nn++
}
}
p = p[:nn]
// fmt.Println(p) i can see the value of p changing and all the null bytes are removed
return n, nil
}
// Close closes the internal reader
func (null RemoveNull) Close() error {
return null.Close()
}
When I run the following I can see from the print statement that indeed all the null bytes are removed and the len(p) == the size of all the expected good bytes. I wrote the test below to see if the code if working as I intended, and that's where I realized it's not.
Here is the full test
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"testing"
"github.com/francoispqt/gojay" // can be replaced with the std json lib, code still doesn't work
)
func TestRemoveNull_Read(t *testing.T) {
type fields struct {
Reader io.ReadCloser
}
tests := []struct {
name string
fields fields
want string
}{
{
name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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 109, 97, 108, 101, 34, 125})),
},
want: "female",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := tt.fields.Reader
reader = NewRemoveNullStream(tt.fields.Reader) // wrapper the reader above in the nullByte reader
// passed the reader into this JSON unmarshaller
decoder := gojay.BorrowDecoder(reader)
defer decoder.Release()
var v _testStruct
err := decoder.DecodeObject(&v)
if err != nil {
t.Fatalf("ReadAll failed %v", err)
}
bb, _ := json.Marshal(v)
fmt.Println(string(bb)) // all the null bytes are still present
fmt.Println(len(v.Body), len(tt.want))
if v.Body != tt.want {
t.Fatalf("DecodeObject() unexpected value, got %s want %s", v.Body, tt.want)
}
})
}
}
type _testStruct struct {
Body string `json:"body"`
}
func (v *_testStruct) UnmarshalJSONObject(dec *gojay.Decoder, k string) error {
switch k {
case "body":
err := dec.String(&v.Body)
return err
}
return nil
}
// NKeys returns the number of keys to unmarshal
func (v *_testStruct) NKeys() int { return 0 }
From the test I can see that all the null bytes are still present when decoding, yet in the RemoveNull reader I can see that all the null bytes have been removed from the underline array. Any thoughts on whats wrong and how i can achieve the goal of removing the bytes from the stream to avoid having the decoder decode the null bytes?
There are errors in your Read implementation. It terminates prematurely in case of io.EOF, where there is both error and data. It returns the wrong number of bytes read. The last part where you assign the slice is also meaningless as it doesn't update the slice passed into the function.
Try this:
func (null RemoveNull) Read(p []byte) (n int, err error) {
n, err = null.Reader.Read(p)
nn := 0
for i:=0;i<n;i++ {
if p[i] != 0 {
p[nn] = p[i]
nn++
}
}
return nn, err
}

how to read multiple lines from serial C, terminated by carriage return AND line feed?

I am working on a project where I need to communicate from an AVR microcontroller to an rs232 device via UART and a TTL level converter. I am using a serial callback to get the characters from the serial port, and checking for a carriage return/new line, then also checking the most recent character in the buffer for carriage return/new line. The data from the rs232 device is coming in the format of 26 lines seperated by carriage return AND line feed comprised of ASCII characters. The entire 26 lines is sent approximately every .7 seconds. I am trying to fill a 2D array with the 26 lines. However, I am now getting a what I should in the receive buffer. Here is my code so far:
volatile uint8_t rxrs_buffer[26][12] = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
};
static void serialrs_rx_cb(const struct usart_async_descriptor *const io_descriptor)
{
//counters
uint8_t ch, count;
//read a character
count = io_read(&SERIALRS232.io, &ch, 1);
//check if we are receiving
if (serialrs_receiving == 0)
{
//check for new line or carriage return
if (ch != 10 && ch != 13)
{
if (rxrs_buffer[row-1][column-1] != 10 && rxrs_buffer[row-1][column-1] != 13)
{ //set receiving flag
serialrs_receiving = 1;
//reset byte counter
serialrs_bytes_received_counter = 0;
//start filling the rx buffer
rxrs_buffer[row][column] = ch;
//increment the byte counter and column
serialrs_bytes_received_counter += count;
column++;
}
}
}
else
{
//continue filling the buffer
rxrs_buffer[row][column] = ch;
//increment the byte counter and column
serialrs_bytes_received_counter += count;
column++;
//check for new line or carriage return
if (ch == 10 || ch == 13)
{
if (rxrs_buffer[row-1][column-1] != 10 && rxrs_buffer[row-1][column-1] != 13)
{
//continue filling the buffer
rxrs_buffer[row][column] = ch;
//increment the byte counter
serialrs_bytes_received_counter += count;
//increment the line counter and reset column
row++;
column = 0;
if (row == 26)
{
//set the completion flag
serialrs_complete = 1;
//total bytes
totalrs_bytes = serialrs_bytes_received_counter - 2;
row = 0;
}
}
}
//check for buffer overflow
if (serialrs_bytes_received_counter >= SERIALRS_BUFFER_SIZE)
{
//reset buffer counter
serialrs_bytes_received_counter = 0;
}
}
}
The issue is my 2D buffer array is not filling up correctly. I am getting rows that start with two line feeds, then some that work perfectly and end in a carriage return line feed, only to find the next row starts with two carriage returns or line feeds? Any suggestions at all would help

Resources