initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

../src/yongqiang.c:29:15: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]

1. Node *head = root;

//============================================================================
// Name        : typedef - struct
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

/*
 ** Insert into an ordered, singly linked list. The arguments are
 ** a pointer to the first node in the list, and the value to insert.
 */

#include <stdio.h>
#include <stdlib.h>

#define	FALSE	1
#define	TRUE	0

typedef struct NODE
{
	struct NODE *link;
	int value;
} Node;

// This function prints contents of linked list starting from the given node.
int sll_display(const Node *root)
{
	Node *head = root;

	if (NULL == root)
	{
		return FALSE;
	}

	printf("\n[ ");

	while (NULL != head)
	{
		printf("%d ", head->value);
		head = head->link;
	}

	printf("]");

	return TRUE;
}

int sll_insert(Node **rootp, int new_value)
{
	Node *current;
	Node *previous;
	Node *new_node;

	if (NULL == rootp)
	{
		return FALSE;
	}

	/*
	 ** Get the pointer to the first node.
	 */
	current = *rootp;
	previous = NULL;

	/*
	 ** Look for the right place by walking down the list
	 ** until we reach a node whose value is greater than or equal to the new value.
	 */
	while ((NULL != current) && (current->value < new_value))
	{
		previous = current;
		current = current->link;
	}

	/*
	 ** Allocate a new node and store the new value into it.
	 ** In this event, we return FALSE.
	 */
	new_node = (Node *) malloc(sizeof(Node));
	if (NULL == new_node)
	{
		return FALSE;
	}
	new_node->value = new_value;

	/*
	 ** Insert the new node into the list, and return TRUE.
	 */
	new_node->link = current;
	if (NULL == previous)
	{
		*rootp = new_node;
	}
	else
	{
		previous->link = new_node;
	}

	return TRUE;
}

int main()
{
	Node *root = NULL;
	int result = 0;
	int ret = 0;

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 5);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 10);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 15);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 9);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 12);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 0);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 20);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	return TRUE;
}

16:47:30 **** Build of configuration Debug for project yongqiang_example ****
make all 
Building file: ../src/yongqiang.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/yongqiang.d" -MT"src/yongqiang.o" -o "src/yongqiang.o" "../src/yongqiang.c"
../src/yongqiang.c: In function ‘sll_display’:
../src/yongqiang.c:29:15: warning: initialization discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
  Node *head = root;
               ^
Finished building: ../src/yongqiang.c
 
Building target: yongqiang_example
Invoking: GCC C Linker
gcc  -o "yongqiang_example"  ./src/yongqiang.o   
Finished building target: yongqiang_example
 

16:47:31 Build Finished (took 595ms)
NULL pointer.
[ 5 ]
[ 5 10 ]
[ 5 10 15 ]
[ 5 9 10 15 ]
[ 5 9 10 12 15 ]
[ 0 5 9 10 12 15 ]
[ 0 5 9 10 12 15 20 ]

2. const Node *head = root;

//============================================================================
// Name        : typedef - struct
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

/*
 ** Insert into an ordered, singly linked list. The arguments are
 ** a pointer to the first node in the list, and the value to insert.
 */

#include <stdio.h>
#include <stdlib.h>

#define	FALSE	1
#define	TRUE	0

typedef struct NODE
{
	struct NODE *link;
	int value;
} Node;

// This function prints contents of linked list starting from the given node.
int sll_display(const Node *root)
{
	const Node *head = root;

	if (NULL == root)
	{
		return FALSE;
	}

	printf("\n[ ");

	while (NULL != head)
	{
		printf("%d ", head->value);
		head = head->link;
	}

	printf("]");

	return TRUE;
}

int sll_insert(Node **rootp, int new_value)
{
	Node *current;
	Node *previous;
	Node *new_node;

	if (NULL == rootp)
	{
		return FALSE;
	}

	/*
	 ** Get the pointer to the first node.
	 */
	current = *rootp;
	previous = NULL;

	/*
	 ** Look for the right place by walking down the list
	 ** until we reach a node whose value is greater than or equal to the new value.
	 */
	while ((NULL != current) && (current->value < new_value))
	{
		previous = current;
		current = current->link;
	}

	/*
	 ** Allocate a new node and store the new value into it.
	 ** In this event, we return FALSE.
	 */
	new_node = (Node *) malloc(sizeof(Node));
	if (NULL == new_node)
	{
		return FALSE;
	}
	new_node->value = new_value;

	/*
	 ** Insert the new node into the list, and return TRUE.
	 */
	new_node->link = current;
	if (NULL == previous)
	{
		*rootp = new_node;
	}
	else
	{
		previous->link = new_node;
	}

	return TRUE;
}

int main()
{
	Node *root = NULL;
	int result = 0;
	int ret = 0;

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 5);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 10);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 15);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 9);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 12);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 0);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	result = sll_insert(&root, 20);
	if (result)
	{
		printf("Failed to insert element.");
	}

	ret = sll_display(root);
	if (ret)
	{
		printf("NULL pointer.");
	}

	return TRUE;
}

16:44:22 **** Build of configuration Debug for project yongqiang_example ****
make all 
Building file: ../src/yongqiang.c
Invoking: GCC C Compiler
gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/yongqiang.d" -MT"src/yongqiang.o" -o "src/yongqiang.o" "../src/yongqiang.c"
Finished building: ../src/yongqiang.c
 
Building target: yongqiang_example
Invoking: GCC C Linker
gcc  -o "yongqiang_example"  ./src/yongqiang.o   
Finished building target: yongqiang_example
 

16:44:23 Build Finished (took 602ms)
NULL pointer.
[ 5 ]
[ 5 10 ]
[ 5 10 15 ]
[ 5 9 10 15 ]
[ 5 9 10 12 15 ]
[ 0 5 9 10 12 15 ]
[ 0 5 9 10 12 15 20 ]
发布了509 篇原创文章 · 获赞 1824 · 访问量 110万+

猜你喜欢

转载自blog.csdn.net/chengyq116/article/details/104863174