Skip to content

Instantly share code, notes, and snippets.

View webgtx's full-sized avatar
:accessibility:
Hope is not a strategy

Alex Zolotarov webgtx

:accessibility:
Hope is not a strategy
View GitHub Profile
@webgtx
webgtx / main.c
Created March 14, 2022 10:36
Arrays & pointer in C
int main() {
int arr[5] = {3, 5, 4, 7}, idx;
for (idx = 0; idx < 5; idx++)
printf("item addr: %i | %p \n", *(arr + idx) ,arr + idx);
return 0;
}
@webgtx
webgtx / matrix.c
Created March 14, 2022 10:38
Matrix in C
void matrix() {
char mtx[2][3] = {{"mat"}, {"rix"}};
mtx[1][1] = '1';
for (int i = 0; i < 2; i++) {
printf("%i)idx: [%c] [%c] [%c]\n", i, mtx[i][0], mtx[i][1], mtx[i][2]);
}
printf("\tsizeof arr = %i bytes\n", sizeof(mtx));
}
@webgtx
webgtx / fs.c
Created March 14, 2022 10:40
How to use file system in C
void wrt_file(char * dat, char * filename) {
FILE *file = fopen(filename, "w");
fprintf(file, dat);
fclose(file);
}
void rd_file(char * filename, char * file_str) {
int c;
int idx = 0;
FILE *file;
@webgtx
webgtx / lnrd.c
Created March 14, 2022 10:41
Secure input read in C
void lnrd(char * str) {
int idx = 0;
while (true) {
char ch = getchar();
if (ch == '\n')
break;
str[idx] = ch;
idx++;
}
str[idx++] = '\0';
@webgtx
webgtx / struct.c
Created March 14, 2022 16:55
How structure works in C
struct author {
int age;
char uname[80];
char stack[80];
};
struct author new_developer() {
struct author new;
printf("Username: ");
lnrd(new.uname);
@webgtx
webgtx / callback.c
Last active April 17, 2022 01:23
Callbacks in C
// Only INT, don't trying use arrays with char
void foreach(int *arr, int len, void (*callback) (int item, int idx)) {
unsigned idx = 0;
for (idx; idx < len; idx++)
callback(arr[idx], idx);
}
int main() {
int arr[] = {1, 3, 5, 6};
int arr_len = sizeof arr / sizeof arr[0];
void cb (int n, int idx) {
@webgtx
webgtx / sys_shell.c
Created April 17, 2022 01:21
How to use system shell in C.
int system(const char *shell);
int main() {
system("whoami");
}
@webgtx
webgtx / atoi.c
Created April 19, 2022 00:18
How to convert string to int. (C)
int main() {
const char str = "We do not cares ;)";
return atoi(str);
}
@webgtx
webgtx / docker.volume.sh
Created May 26, 2022 18:50
docker volume control
# create the volume in advance
docker volume create --driver local \
--opt type=none \
--opt device=/home/user/test \
--opt o=bind \
test_vol
# create on the fly with --mount
docker run -it --rm \
--mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test \
@webgtx
webgtx / reverse_str.c
Last active February 3, 2023 05:43
How to reverse string in C
#include <stdio.h>
#include <string.h>
// My solution
void rvrs(char * str, char * reversed_str, unsigned len) {
str += len - 1;
while (str[0]) {
*reversed_str = str[0];
str -= 1;
reversed_str++;