Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
3  
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
4  
@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
1  
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
1  
@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
show 2 more comments

4 Answers

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
1  
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
add comment

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
add comment

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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