Posts

Showing posts with the label struct

How to create a matrix structure in C?

How to create a matrix structure in C? I'm a beginner in C, but I'm currently trying to create a matrix data structure that could be used in different functions without having to explicitly pass the number of columns and number of rows (example: matrixMult(matrix A, matrix B) instead of matrixMult(A, B, rowsA, columnsA, rowsB, columnsB) ). My approach so far has been to declare a struct such as matrixMult(matrix A, matrix B) matrixMult(A, B, rowsA, columnsA, rowsB, columnsB) typedef struct matrix{ int rows; int columns; int **data; }matrix; And then, to properly allocate the matrix, I'm trying to use the following function matrix startmatrix(matrix mat,int n_row, int n_col){ int i=0; mat.rows=n_row; mat.columns=n_col; mat.data=(int **) calloc(n_row,sizeof(int *)); for(i=0;i<n_row;i++){ mat.data[i]=(int *) calloc(n_col,sizeof(int)); } return mat; } Which (to my understanding) allocates the rows containing the columns, and ...

Reading a tcp packet on Node js server

Reading a tcp packet on Node js server I am trying to read a tcp packet on node js sent from c++ tcp client. Below is the struct sent by tcp client struct LoginPacket { uint16_t packetLength; // value:24 uint8_t cmd; // value: 96 uint8_t version; // value: 1 uint8_t checkSum; // value: 1 uint8_t type1; // value: 1 uint8_t type2; // value: 1 uint8_t random; // value: 8 uint8_t lengthOfText; // value: 7 char *textField; // value: "abcd" }; Below is my nodejs server application to read this struct. var tcpSock = require('net'); var server = tcpSock.createServer(function(socket) { socket.write('Echo serverrn'); socket.pipe(socket); //var buffer = new Buffer(4096, 'hex'); // listen for incoming data socket.on("data", function(data){ buf = new Buffer(256); len = buf.write(data.toString()); var Struct = require('struct').Struct; function getPacketHeader(buffer){ var packetHeader = new Struct() ...