I am trying to run this code on my local machine, but I am receiving the following errors:
ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings] error in my Terminal
File is not compatible with the version of Windows you're running. Check your computer's system information and then contact the software publisher.
These erros are blocking me compiling the code, I am using C | Visual Studio Code | Windows 10 and MSY2 Ming64w to compile my code - can you help me solve this?
my.functions.h
#ifndef HW1
#define HW1
#include <stdio.h>
int str_length(char*);
void str_copy(char* src, char* dest);
int str_find(char* src, char* dest);
int get_unnamed_argument(int index, int argc, char **argv, char *result);
int get_named_argument(int index, int argc, char **argv, char *result);
#endif
my_fuctions.c
#include <stdio.h>
#include "my_functions.h"
int str_length(char* string) {
int c = 0;
for (c = 0; string[c] != '\0'; c++);
return c;
}
void str_copy(char* d, char* source)
{
int i;
for (i = 0; source[i] != '\0'; i++)
{
d[i] = source[i];
}
d[i] = '\0';
}
int str_find(char* needle, char* haystack)
{
int c,x;
for(c = 0; c < str_length(haystack); c++){
if (needle[0] == haystack[c]){
int count = 0;
for(x = 0; x < str_length(needle); x++){
if(needle[x] == haystack[c+x]){
count++;
}
}
if (count == str_length(needle)){
return c;
}
}
}
return -1;
}
int get_unnamed_argument(int index, int argc, char **argv, char *result) {
int c;
for (c = 0;c <= index;c++) {
if (index > argc - 1) {
return -1;
}
else if (str_find("=",argv[c]) != -1) {
index++;
}
else if (str_find("--",argv[c]) != -1) {
return -1;
}
else if (c == index) {
str_copy(result,argv[c]);
return str_length(argv[c]);
}
}
return -1;
}
int get_named_argument(int index, int argc, char **argv, char *result) {
int c;
for (c = 0; c <= index; c++) {
if (index > argc - 1) {
return -1;
}
else if (str_find("=",argv[c]) == -1) {
index++;
}
else if (str_find("--",argv[c]) != -1) {
return -1;
}
else if (c == index) {
str_copy(result, argv[c]);
return str_length(argv[c]);
}
}
return -1;
}
main.c
#include <stdio.h>
#include "my_functions.c"
#include "my_functions.h"
int main(int argc, char **argv) {
char t[1];
int counter;
scanf("%s %i", t, &counter);
if (str_length(t) == 1 && str_find("n", t) != -1)
{
if (counter <= 0)
{
counter = argc;
}
int c;
for (c = 0; c < counter; c++)
{
char result[255];
int arg_len = get_named_argument(c, argc, argv, result);
if (arg_len == -1)
{
return 0;
}
printf("%s\n", result);
}
} else if (str_length(t) == 1 && str_find("u", t) != -1)
{
if (counter <= 0)
{
counter = argc;
}
int c;
for (c = 0; c < counter; c++)
{
char result[255];
int arg_len = get_unnamed_argument(c, argc, argv, result);
if (arg_len == -1)
{
return 0;
}
printf("%s\n", result);
}
} else {
if (counter <= 0)
{
counter = argc;
}
int c;
for (c = 0; c < counter; c++) {
if (str_find("--", argv[c]) != -1)
{
return 0;
}
printf("%s\n", argv[c]);
}
}
return 0;
}