|
I'm working on a C project, but all my experience is with C++. Any recommendations for a good C website? Preferably with a good, active community like CodeProject's for questions I might have.
Thanks
|
|
|
|
|
Mark J. Miller wrote: Any recommendations for a good C website?
I'm a pretty avid C programmer myself and I tend to find a lot of use out of CP still. Granted. I can never use much code from most articles as is (not that I would anyway), but the concepts are generally translatable.
What in particular are you after? The C way of life? Differences between C and C++ (like how myFunc(void) is interpreted differently between the two for instance), etc.?
Actually, if you're used to C++ I think it'll benefit you in a procedural world. I think employing some OOP concepts in C (within reason) is a better way of going about things than most traditional procedural texts bother to teach (they never focus on design IMO).
|
|
|
|
|
I have a school project which includes part of the code already written in C. I either need to translate what has been written into C++ or I need a good reference which can tell me how to do in C what I already know in C++.
|
|
|
|
|
I have found on several occasions that doing even a simplistic ( one file == one class ) port of C code to C++ has helped me to understand the code much better than any amount of time spent simply reading it. There are also numerous potential advantages in porting the code from spotting bugs to taking advantage of newer technologies. There's always the risk of introducing new bugs of course but most of this can be offset by being able to test in direct comparison to the original code. If it was me I'd probably C++ the code as straight forwardly as possible, make it work, and then look at ways to improve it if the schedule allowed.
Nothing is exactly what it seems but everything with seems can be unpicked.
|
|
|
|
|
Mark J. Miller wrote: I either need to translate what has been written into C++ or I need a good reference which can tell me how to do in C what I already know in C++.
Actually, you're in luck, one of the design goals of C++ was to be able to compile C code. However, if the program wasn't written with some OOP principles (like treating a file.c as a class, etc.) then a direct port may be an issue.
For the most part you'll feel at home in C, but there are a few gotchas along the way. Here's one link for example:
http://people.cs.uchicago.edu/~iancooke/osstuff/ccc.html[^]
|
|
|
|
|
I am surprised no one pointed this out. If you want to include C code into a C++ project then wrap the C headers in an extern “C” {headers here} block or place the block in the header files themselves (Hint: See the C header file included with C++).
As an alternative you can change the names of the C sources files from “.c” to “.cpp” and they will be compiled as C++. Since C++ is a superset of C, they should compile without any problems. There are some differences you need to look out for, but if you do not need to modify the C code it should be ok. If you need to modify the C code, then either use the block method or learn the minor differences (Hint: sizeof works differently in C).
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Mark J. Miller wrote: Any recommendations for a good C website?
What's wrong with this one? If you are already familar with C++, then there's very little about C that will surprise you.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
May be we should ask Chris to rename the forum as C/C++/VC++
Jemmy : Deadline? Pfft, a real programmer eats deadlines for breakfast. :P
Mark: I thought real programmers ignored deadlines
Best wishes to Rexx[^]
|
|
|
|
|
As always I got what I needed. You guys at CP are the best.
|
|
|
|
|
You've got any questions with Assemblers?
Jemmy : Deadline? Pfft, a real programmer eats deadlines for breakfast. :P
Mark: I thought real programmers ignored deadlines
Best wishes to Rexx[^]
|
|
|
|
|
|
Is there a way to serialize an enumerated type using CArchive?
// I have an enumerated type in my header file and a member of that type,
typedef enum
{
ONE,
TWO,
THREE
} SomeType;
SomeType m_someVariable;
When I try to serialize it in the classes serialize method, I get...
error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'CSomething::SomeType' (or there is no acceptable conversion)
|
|
|
|
|
easiest way is to cast the enum to an int when saving, and cast the int to an enum when loading.
|
|
|
|
|
That's not working for me here. Are you on 6.0 or 2003.
I'm going to try this on a VC++ 6.0 project to see what happens as my current project is in VC++ 2003.
Thanks for the advice either way.
|
|
|
|
|
bob16972 wrote: Are you on 6.0 or 2003
doesn't matter. this is a pretty basic C++ issue, the kind of thing you run into everywhere.
int t;
ar >> t;
enumVal = (enumType)t;
ar << (int)enumVal;
|
|
|
|
|
My apologies.
I would never have suspected that was necessary but I live and learn.
Thank you very much for helping me over that hurdle.
|
|
|
|
|
Do you have something similar to:
class CSomething
{
public:
enum
{
ONE,
TWO,
THREE
} SomeType;
void Serialize( CArchive &ar )
{
if (ar.IsStoring())
ar << (int) SomeType;
else
ar >> (int) SomeType;
}
};
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
That's basically it. I needed a typedef in front of the enum. I've tried a couple variations which should effectively be the same thing in the end but none get me past the error.
I tried the cast operation an it didn't change the error.
I'm not sure if this matters, but I've noticed from past posts you use VC++ 6.0 exclusively and this project is on Visual C++ 2003, so if that's working for you in your environment, I'll give it try in a Visual C++ 6.0 project to see if the 2003 compiler just changed the rules.
|
|
|
|
|
bob16972 wrote: ...if that's working for you in your environment, I'll give it try in a Visual C++ 6.0 project...
You might have to tweak it a bit like:
if (ar.IsStoring())
{
int x = SomeType;
ar << x;
}
else
{
int x;
ar >> x;
SomeType = (_SomeType) x;
}
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I feel silly now but a tad bit smarter.
Thanks for helping me out. I learned something new.
|
|
|
|
|
Why have you written the underscore in the casting by loading???
I mean: SomeType = (_SomeType) x;
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Nelek wrote: Why have you written the underscore in the casting by loading???
To differentiate between a type and a value.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Thanks
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|
|
Hi,
Is there a possibility to get the previous item selection in a LVN_ONITEMCHANGING handler.
The handler provides the item that is about to change from unselected to selected, but I can not
find a way to get the item that loses the selection.
void CMPRSDlg::OnItemChangingMPRSLst(NMHDR *pNMHDR, LRESULT *pResult)
{
NM_LISTVIEW *pNMListView = (NM_LISTVIEW *) pNMHDR;
*pResult = 0;
// TODO : Add your control notification handler code here.
if (pNMListView->uChanged & LVIF_STATE)
{
int nItem = pNMListView->iItem;
// An item's state is about to change.
if (!(pNMListView->uOldState & LVIS_SELECTED) && (pNMListView->uNewState & LVIS_SELECTED))
{
// Item will go from unselected to selected.
ASSERT(::IsWindow(m_ctlMPRSLst.m_hWnd));
POSITION pos = m_ctlMPRSLst.GetFirstSelectedItemPosition();
/// THIS DOES NOT WORK. pos = NULL ???? Is there another way ?
:
:
// Prevent selection if the item is locked.
CMPRSEntry *pEntry = (CMPRSEntry *) m_ctlMPRSLst.GetItemData(nItem);
if (pEntry->m_SKU.IsLocked())
{
*pResult = 1;
return;
}
}
}
}
|
|
|
|
|
If the number of items is not very big and the time is not critical you can deal with it using a help-array.
I mean:
void CMPRSDlg::OnItemChangingMPRSLst(NMHDR *pNMHDR, LRESULT *pResult)
{
for (int i = 0; i < number_of_items; i++)
{
if (Help_array[i] == 1)
{
last_item = i;
Help_array[i] = 0;
break;
}
last_item = -1;
}
Help_array[index_of_changing_item] = 1;
}
Where Help_Array has zero in ALL positions at the beggining.
Hope it helps.
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
|
|
|
|