|
I used to write things like 2[array] to wind up certain colleagues.
(Often the ones that insisted on putting the constant on the LHS of a comparison operator.)
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Peter_in_2780 wrote: the ones that insisted on putting the constant on the LHS of a comparison operator.
|
|
|
|
|
No real preference, I use whichever seems (to me) to make most sense at the time.
|
|
|
|
|
If the data type is defined as an array then use it as array in expressions.
If the data type is defined as a pointer then use it as a pointer in expressions.
honey the codewitch wrote: some people prefer &foo[bar]
Rather certain that I am one that would not want to see that. I work to write code that is maintainable and using tricks just because the exist is not something that I look on favorably.
|
|
|
|
|
That's not really a trick.
It's "get me the address of the Nth array element."
It's just another way of writing (foo + bar);
If you insist on not mixing pointer and array ops than you'd be stuck with the very syntax you don't like in order to get the address of an array element, unless there's a 3rd way to do it I'm not considering?
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
So you say it is an array. Then use it as an array when you identify the element you want to take the address of.
|
|
|
|
|
that's exactly what &foo[bar] does.
Literally it reads as take the address of the bar element of foo.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
Right. So use that, rather than (foo + bar).
|
|
|
|
|
honey the codewitch wrote: It's just another way of writing (foo + bar);
yes I know.
honey the codewitch wrote: If you insist on not mixing pointer and array ops
Not sure what you mean.
If I create an array of pointers then I would use it like an array.
If I create a pointer to memory that contains sequential blocks (regardless of type) then I would use it like a pointer.
The fact that syntactically the access is the same and tradable is not a consideration.
|
|
|
|
|
jschell wrote: Not sure what you mean.
I mean this
jschell wrote: If I create an array of pointers then I would use it like an array.
If I create a pointer to memory that contains sequential blocks (regardless of type) then I would use it like a pointer
So I think somewhere we started talking past each other.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
&(foo[bar])
makes it real obvious you want the address of the barth element of array foo
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|
|
fair enough.
Edit: To me the operation is obvious, such that using parentheses never even occurred to me. Clearly that's not the case with the other people that have commented here. Not sure what that says about my code.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
It's obvious to me too, but I've been coding C since I upgraded from assembler back in the late 80's . Those who aren't as strong in C that have looked at my code always find it easier to understand pointer manipulation if I use array notation with those redundant (). They have more difficulty with foo + bar -- the idea that foo + 1 moves the pointer by (size of what foo points) bytes instead of 1 byte always blows their mind.
Be wary of strong drink. It can make you shoot at tax collectors - and miss.
Lazarus Long, "Time Enough For Love" by Robert A. Heinlein
|
|
|
|
|
|
|
Did you intend to put an 'Enter' between 'foo' and 'bar'?
If the comma was intended as an 'Enter', I guess that the comma between, 'bar' and '+' would have fouled it up.
|
|
|
|
|
That is wrong, yes ... I was letting it slide because I was so late in on the thread that I figured no one would notice. Is it me or has the complexion of such changed lately? Anyway.
What I meant to type was "foo,bar+". I'm no programmer just an antique calculator button presser.
|
|
|
|
|
One of the things that pisses me off in this book I bought about programming GPUs is that he presents so called "pseudocode" example for presenting his solutions. This pseudocode makes extensive use of C style pointer arithmetic, such as (foo + bar) without a word of mention; the reader is supposed to understand what it means, regardless of his programming background or preferred language.
I do have enough background to understand it when I see it. But I see pseudocode as abstracted above one specific programming language, not just as an excuse for ignoring specific requirements of one chosen language. (foo+bar) is something you'll never find in any comparable language. It doesn't belong in any abstracted pseudocode.
So does it belong in concrete code in one specific language?
Well, if you strive to establish a tribal code, to distinguish between those who are in and those outside, this may be one of your tools. Those who say 'But that is an array, FGS! - Why don't you treat it as one?' - and recoil in horror. Maybe those could have been good developers for your project.
|
|
|
|
|
In Visual Studio (latest), C++ solution.
I have a project like :
project\app
project\lib1
project\lib2
project\lib3
project\util
app <-- lib1 <-- util
<-- lib2
<-- lib3
lib1 is dependant on util and app is dependant on lib1.
util needs to be build before lib1 and lib1 before app.
In the project dependencies window, I select lib1 and then check the util dependency.
I've also looked at the project references and my lib1 is setup correctly to reference the util library.
When rebuilding lib1, I expected util to be build before.
There must be something I'm missing. (obviously)
Thanks.
CI/CD = Continuous Impediment/Continuous Despair
modified 15-Nov-23 13:45pm.
|
|
|
|
|
I assume you're using VS. I never had a problem with it once dependencies were set up correctly, although I migrated builds to CMake last year. The only thing I can think of is that if lib1 changed but not util, then the build can just start with lib1.
|
|
|
|
|
Thanks,
I'm in the process of physically moving libraries around (don't ask) and cleaning up the solution.
When I rebuild from scratch, I just want to build the util library first so that all other libraries and eventually the app can build.
I don't think I can change to cmake (that would require a revolution).
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
CMake is integrated with VS now. Migrating to it (about 20 libraries) wasn't anywhere near as hard as I anticipated, and builds are much faster under Ninja. If you're interested, here's[^] an overview.
|
|
|
|
|
I'm working in an antediluvian organisation.
Eventually we'll do it. (or not)
CI/CD = Continuous Impediment/Continuous Despair
|
|
|
|
|
Every organization is antediluvian!
|
|
|
|
|
Project dependencies can be changed by going to the Solution properties (right click on the solution explorer root of the tree)
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|