whats the meaning of [Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] ?
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct list_entry//typedef:replace old data types into new data types
{
char *strings;
size_t len;//represent size of variable in bytes,
//big enough to represent size of biggest object that system can handle
list_entry *next;
}list_entry;
typedef struct list
{
list_entry *head;
list_entry *tail;
}list;
/*static*/ void initiate(list *lists) //static: if restrict access, then make static, eg: no other file can access this function except file is declared
{
unsigned int n;//unsigned: no negative number (in ASCII table)
for (n = 0; n < 256; n++)//256 represent all characters in ASCII tables
{
lists[n].head = NULL;
lists[n].tail = NULL;
}
}
/*static*/ size_t init_entries(char **strings, size_t len, list_entry *entries)//double pointer, address of strings
{
unsigned int i;//unsigned: no negative number (in ASCII table)
size_t maxlen = 0;//initialize max length as 0
for (i = 0; i < len; i++)//looping
{
entries[i].strings = strings[i];
entries[i].len = strlen(strings[i]);//size of string [strlen()]
if (entries[i].len > maxlen)//if entries length more than max length
{
maxlen = entries[i].len;//curr max length will replace by curr entries length
}
if (i < len - 1)// assume i=0 and len=8, 0<7
{
entries[i].next = &entries[i + 1];//0 linked list add next will be 1 linked list add
}
else//if > 7,[8],
{
entries[i].next = NULL;//8 linked list add will be NULL, cause only total 8 elements
}
}
return maxlen;// for 1st if statement, if didn't match, then max len will not be replaced, and return the original max
}
void list_strings(list_entry *head, list *lists, unsigned int c)
{
int position;
unsigned char a;
list_entry *curr = head;
while (curr != NULL)//if current is not equal to NULL
{
list_entry * next = curr->next;
curr->next = NULL;
position = curr -> len - c;
if (position < 0)
{
a = 0;
}
else
{
a = curr->strings[position];
}
if (lists[a].head == NULL)
{
lists[a].head = curr;
lists[a].tail = curr;
}
else
{
lists[a].tail->next = curr;
lists[a].tail = lists[a].tail->next;
}
curr = next;
}
}
list_entry *merge_lists(list *lists)
{
list_entry *head = NULL;
list_entry *tail;
unsigned int n;
for (n=0; n<256; n++)
{
if (lists[n].head != NULL)
{
if (head == NULL)
{
head = lists[n].head;
tail = lists[n].tail;
}
else
{
tail->next = lists[n].head;
tail = lists[n].tail;
}
}
}
return head;
}
void print_list(const list_entry *head)
{
const list_entry *current;
for (current = head; current != NULL; current = current->next)
{
printf("%s", current->strings);
if (current->next != NULL)
{
printf("\t");
}
}
printf("\n\n");
}
void copy_list(const list_entry *head, char **strings)
{
const list_entry *current;
unsigned int s;
for (current = head, s = 0; current != NULL; current = current->next, s++)
{
strings[s] = current->strings;
}
}
void radix_sort_string(char **strings, size_t len)
{
size_t maxlen;
unsigned int c;
list_entry *head;
list_entry *entries = (list_entry*)malloc(sizeof(list_entry) * len);
list *lists = (list*)malloc(sizeof(list) * 256);
if (!entries || !lists)
{
free(entries);
free(lists);
return;
}
initiate(lists);
maxlen = init_entries(strings, len, entries);
head = &entries[0];
for (c = 1; c <= maxlen; c++)
{
printf("Sorting on char %d from the right:\n", c);
list_strings(head, lists, c);
head = merge_lists(lists);
print_list(head);
initiate(lists);
}
copy_list(head, strings);
free(lists);
free(entries);
}
int main()
{
char *strings[]={"kitten","puppy","sheep","birds","mouse","snake","ants"};
const size_t len = sizeof(strings) / sizeof(char*);
size_t maxlen;
radix_sort_string(strings, len);
return 0;
}