Skip to content

Instantly share code, notes, and snippets.

@ifindev
Last active February 4, 2021 04:17
Show Gist options
  • Save ifindev/a368718db1f40f3f7b338424efbd391a to your computer and use it in GitHub Desktop.
Save ifindev/a368718db1f40f3f7b338424efbd391a to your computer and use it in GitHub Desktop.
C & C++ Exploratory Codes: Part-1

C & C++ Exploratory Codes: Part-1

These are some of my experimentation codes I made while trying to understand F-Army & Thotro Arduino Library for DWM1000 module. It might be useful for future references, I think.

/*
Run this on: https://www.onlinegdb.com/
*/
#include <iostream>
#include <cstring>
typedef uint8_t byte;
using namespace std;
void writeValueToBytes(byte data[], uint64_t val, uint8_t n);
uint64_t bytesAsValue(byte data[], uint8_t n);
double x = 100000.7;
double y = 200.5;
int main()
{
byte report[] = {0,0,0,0};
uint32_t pos = static_cast<uint32_t>(x);
writeValueToBytes(&report[0], static_cast<uint32_t>(x*1000), 4);
cout << pos << endl;
cout<<"Hello World"<<endl;
printf("\n");
for(int i = 0; i < 4; i++){
printf("%#X\n", report[i]);
}
double pos_x = static_cast<double>(bytesAsValue(&report[0], 4));
cout<<"Pos_x: " << pos_x <<endl;
return 0;
}
void writeValueToBytes(byte data[], uint64_t val, uint8_t n) {
for(auto i = 0; i < n; i++) {
data[i] = ((val >> (i*8)) & 0xFF);
}
}
uint64_t bytesAsValue(byte data[], uint8_t n) {
uint64_t value = 0;
for(auto i = 0; i < n; i++) {
value |= ((uint64_t)data[i] << (i*8));
}
return value;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int check(int a, int b) {
return a == b;
}
int main()
{
int top = 1;
int cap = 1;
int nil = 0;
int res1 = check(top, cap);
int res2 = check(top, nil);
printf("Res1: %d\n", res1);
printf("Res2: %d\n", res2);
return 0;
}
#include <stdio.h>
int main()
{
int *ptr, p;
p = 22;
printf("Address of p: %p\n", &p);
printf("Value of p: %d\n\n", p); // 22
ptr = &p; // pointer to address of p
printf("Address of pointer ptr: %p\n", ptr);
printf("Content of pointer ptr: %d\n\n", *(ptr)); // 22
*ptr = 3;
printf("Address of p: %p\n", &p);
printf("Value of p: %d\n\n", p); // 22
return 0;
}
/******************************************************************************
Welcome to GDB Online.
*******************************************************************************/
#include <stdio.h>
typedef struct Complex {
int imag;
float real;
} Complex;
typedef struct Number{
Complex comp;
int integers;
} Number;
int main()
{
Number number, num_array[3];
number.comp.imag = 11;
number.comp.real = 10.5;
number.integers = 12;
printf("Imaginary number: %d\n", number.comp.imag);
printf("Real number: %.2f\n", number.comp.real);
printf("Integers: %d\n\n", number.integers);
for(int i=0; i < 3; i++){
num_array[i].comp.imag = i+1;
num_array[i].comp.real = (float)(i+1.5);
num_array[i].integers = i+3;
printf("Imaginary number: %d\n", num_array[i].comp.imag);
printf("Real number: %.2f\n", num_array[i].comp.real);
printf("Integers: %d\n\n", num_array[i].integers);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
ptr = (int*)malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL){
printf("Error! Memory not allocated");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; i++){
scanf("%d", ptr + i);
sum += *(ptr + 1);
}
printf("Sum = %d", sum);
// deallocating free Memory
free(ptr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment