tags:

views:

686

answers:

1

I'm trying to print the list of a singly linked list that I referred to in link text

It works, but I do get the compiler warnings

"Initialization discards qualifiers from pointer target type"(on declaration of start = head) and return discards qualifiers from pointer target type"(on return statement) in this code (I am using XCode):

/* Prints sinly linked list and returns head pointer */
LIST *PrintList(const LIST *head) 
{
    LIST *start = head;

    for (; start != NULL; start = start->next)
        printf("%15s %d ea\n", head->str, head->count);

    return head;
}

Any thoughts? Thanks!

+4  A: 

It's this part:

LIST *start = head;

The parameter for the function is a constant pointer, const LIST *head; this means you cannot change what it is pointing to. However, the pointer above is non-const; you could dereference it and change it.

It needs to be const as well:

const LIST *start = head;

The same applies to your return type.


All the compiler is saying is: "Hey, you said to the caller 'I won't change anything', but you're opening up opportunities for that."

GMan
Dumb question, but what does a const return type look like? I tried searching on the web, and I can't seem to find one.
Crystal
@Crystal - `const LIST *PrintList(const LIST *head) { ... }`
R Samuel Klatchko