Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In C, I often come across errors with code like this

prio_queue->head[index] = newEntry;  

Yields a " "Cannot assign Entry** to Entry* " How do you prevent/debug errors of this type, in general?

share|improve this question

closed as too broad by MichaelT, Snowman, durron597, GlenH7, Ixrec Sep 2 '15 at 18:43

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

This is syntactically correct. What are the types and data structures, what error do you think you have? Need more information..... – mattnz Feb 27 '12 at 0:08
The error was "Cannot assign Entry** to Entry* " – Adel Feb 27 '12 at 0:09
@Adel, you're lucky - it's one of the rare cases when the C compiler managed to find an error for you. What else do you need? – SK-logic Feb 27 '12 at 13:06
Huh? Just don't make such errors. And if you did, be happy the compiler found it and correct it. What else do you want to hear? Don't use C? But well, another language doesn't completely protect you from the burden of actually understanding what you do. – Christian Rau Feb 27 '12 at 17:27
@Adel Sorry, it may have been a bit harsh, but I just couldn't get the point of this question in any way. – Christian Rau Feb 27 '12 at 17:40
up vote 3 down vote accepted

If head is an array of elements, and index is < size of that array, and newEntry is of the same type as the array, and prio_queue is a pointer to a data structure containing head, then there nothing wrong with this code.

A lot of ifs, but without knowing your data structures, I (and no one else) can tell if it's right or wrong.

Update: Based on the update to the question, a strategy would be to keep careful track of how many levels of indirection a variable has.

share|improve this answer
" track of how many levels of indirection a variable" - hmm, this suggests using paper & pencil to double-check. Which seems smart to me, thanks! – Adel Feb 27 '12 at 17:31
Paper is a bad way to go. Use variable naming conventions where appropriate (Not application Hungarian, descriptive names), and typedefs. – mattnz Feb 27 '12 at 21:23

There are two types of problems you may run into:

  1. The compilation level where pointer data types are not matching

  2. The run time issues where array bounds overflows and hence either violates the memory access or corrupts the internal data structure which will result in memory problem later in the execution.

Your problem looks like compilation problem than run time so it looks more like data type mismatch problem mostly but we need more information to dig up this. Please elaborate.

share|improve this answer

I've been away from a C compiler for quite a few years, but isn't it telling you that prio_queue->head[index] is a pointer to Entry and newEntry is a pointer to a pointer to Entry? Try:

prio_queue->head[index] = *newEntry;

share|improve this answer
This will fix the syntax error, however no one can say if it is correct. Given the question, I have my doubts...... – mattnz Feb 27 '12 at 21:24

How do you prevent/debug errors of this type, in general?

This particular issue comes down to knowing the types of the respective expressions, and there are no shortcuts to that; either you know it or you don't. The question to answer is why the types are different in the first place; that indicates that someone was a bit confused when they wrote the code.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.