|
Bad practice is using such chains of pointers. You should check if that can be avoided. If structure members can share memory locations, you should use unions.
I would use a variable instead to make the code more readable and avoid the chain:
struct4_t *struct4 = struct1->struct2->struct3->struct4;
s5val = struct4->struct5->value;
|
|
|
|
|
Thanks for your answer Jochen. I have been thinking better and maybe I will accept the Richard suggestion because the real situation is something worst
struct1->struct2->struct3->struct4[struct5->struct6->struct7]
Not pretty ah?.
Best regards
Filipe Marques
|
|
|
|
|
Very bad practice, it makes it difficult to understand or maintain the code; you should look closely at your design. You may also benefit from upgrading to C++ and using classes instead of structs.
Veni, vidi, abiit domum
|
|
|
|
|
Hi Richar, thanks for your answer. Yeah, the code is getting realy hard to maintain and each little change I do, it is a nightmare.
Best regards
Filipe Marques
|
|
|
|
|
Not sure if some C-Compilers accept functions within a struct like in C++ Classes, but if not, you could just define a global function instead of a member function. You could then also integrate some sanity checks to make your code safer, e. g.:
Struct4* haul(Struct1* s1) {
if (s1 && s1->struct2 && s1->struct2->struct3 && s1->struct2->struct3->struct4)
return s1->struct2->struct3->struct4;
else
return 0;
}
...
Struct4* s4 = haul(struct1);
if (s4) {
s4->struct5->struct6;
...
}
Of course the question is why you need so many levels of indirection
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
|
Hi Stefan. Thanks for your answer. It is very smart way to contour the problem. I think I would never figure out a solution like that . The reason of so many levels is because the s1 is an identity with a lot of different information and I must have access to them in every function (I thought this was the best way) but I will take a chance in C++. I think in my situation will help me a lot.
Best regards
Filipe Marques
|
|
|
|
|
You appear to be facing a problem that is not related to the programming language or it's (lack of) features: when your function needs to parse a data structure of such complexity, the question arises why it needs to know about the entirety of this structure? Wouldn't it be sufficient if e. g. the caller passed struct1->struct2->struct3 to the function as an argument, so it wouldn't need to dig through the top layers to start with? If this is true, then you need to redesign the function signature so it requests only pointer(s) those parts of the data structures that it needs to access.
Or, if your function really needs to know about all parts of that data structure, then it might be doing too much in just one function, and you should consider splitting it up into multiple parts.
Or, if your function is small, and really can't be split up any further in a sensible way, then the question arises why are these data values spread so far out over different parts of the data structure? Maybe it is the data structures after all , that need to be redesigned, so that related values are stored within the same (sub-)structure!
All of this is based on the principals of "locality of concern" or "information hiding". You can google on those expressions for more information.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
Thank you Stefan, I didn't know about "locality of concern" or "information hiding". In these couple of days I have been review the code and yes, I am passing information that I am not really using in my functions and I am making all of them available to the user, unnecessarily. Right now I am redrawing the code ... again
Best regards
Filipe Marques
|
|
|
|
|
It becomes a "depends" question and by that I mean it depends on why such deep nesting is required. To me your code looks like it either needed a variant record or simple database functions put over the top of it.
To give you the idea lets look at a simple example that would nest something like what you have. Lets say we have database of books that were linked in memory. Done in your sort of nested format to access a given line would be like this
section struct->book struct->page struct-> Line struct->actual text on that line
So you could imagine an example something like
Fiction->War and Peace->page 200->line 20->text
That structure points to the text on the 200th page, line 20 of the fictional book called "war and peace"
There is nothing wrong with that but from a programming sense it would have been much more desirable to setup a basic interface.
Define your structures Section, Book, Page, Line as per normal but make functions to do the work
book* SelectBook (book* Booklist, section, title);
page* MoveToPage (book* SelectedBook, int PageNum);
line* MoveToLine (page* SelectedPage, int LineNum);
So your code would now become something like
book* MyCurrentBook;
page* CurrentPage;
line* CurrentLine;
MyCurrentBook = SelectBook(BookList, Fiction, "War and peace");
Currentpage = MoveToPage(MyCurrentBook, 200);
CurrentLine = MoveToLine(Currentpage, 20);
What you get for the cost of building the functions is code clarity as well as being able to contain error checks inside the functions. You can take the idea even further if you like and start writing object like code in C.
So my ultimate view of your macro depends on what your nested structure list is for and could it have been done a better way. If it couldn't then using the MACRO is perfectly fine.
|
|
|
|
|
Hi Leon, thanks for your answer. What you described here is exactly my problem. The problem is that I always program in C++ and the few C programs were really small ones. The way to think is a little different and I always think in an object way. I decided to migrate to C++. The code became cleaned and easy to read. One more time, thanks for your answer and I am sorry to take so long to answer Best regards Filipe Marques
|
|
|
|
|
Hi!
I am a new one for c++. I have to plane create a new adblock application for the browsers(Firefox, Chrome and safari). I don't have any idea. Please anyone help me and guide me. Its urgent.
|
|
|
|
|
It would probably be best to create add-ins for the different browsers. You'll have to research and study how to create add-ins for Firefox, Chrome and Safari.
I know that to develop for the Apple platform requires you to join their developer program which costs money.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Is there anyway to do this app?
|
|
|
|
|
Of course there is. But you can't learn how from some forum posts. You have to study how to create add-ins for the specific browsers.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Thanks for your reply...
I will study.
Thanks again.
|
|
|
|
|
Seems someone down voted the question so I up voted it just to offset.
Member 8041178 wrote: I am a new one for c++.
If you are really new to programming and to C++ then I suggest you start with something which will have a smaller scope.
For example attempt to create your own browser first. Or your own server that your browser can connect to and display something.
|
|
|
|
|
jschell wrote: Seems someone down voted the question so I up voted it just to offset.
You up-voted a question which basically reads: "I want to write an application to do X, but I don't know how. Someone do it for me. It's urgent!"?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: Someone do it for me. I
I don't consider "Please anyone help me and guide me" to be the same as "do it for me". Not even really close.
And I basically ignore "urgent" requests so intent there means little.
|
|
|
|
|
Hi All:
I wanna implemented a application with MFC, when clicke a button on any application, select a icon on the desktop then play sound.
I try to implement similar function with ON_WM_LBUTTONDOWN(), but not satisfied my request. If I click everywhere on desktop, it will play sound too.
How do I implement this application? Does any keyword or reference?
My English is not good. Please forgive me.
Best regards, Victor
|
|
|
|
|
|
But, I added WM_LBUTTONDOWN into Mousehookproc, it does not work.
which do I add in, WinProcHook or Mousehookproc? if I add into WinProcHook, how can I get the button?
Thanks for your help, Victor
|
|
|
|
|
You'll have to use the concept of 'hook'.
Please refer to hook details here. You can use this[^] article for your reference.
|
|
|
|
|
But, I added WM_LBUTTONDOWN into Mousehookproc, it does not work.
which do I add in, WinProcHook or Mousehookproc? if I add into WinProcHook, how can I get the button?
Thanks for your help, Victor
|
|
|
|
|
Please verify that the Hook got installed properly. Once the hook is installed, you should be able to get the messages.
When the mouse event is received, you can check the window handle. Using that window handle, identify the class type of the window.
|
|
|
|