I'm now teaching myself C for the purpose of programming microcontrollers. I want to write to members of a structure in a sequential fashion, and I want to do this in a loop where the index points to each member. So for example, I have a structure: code:
I've learned about how pointers can address members directly, but I haven't found any examples of how the member being pointed to can be decided by a variable. In my mind it looks like this: code:
And I do have a good reason for using a structure instead of an array; it would really help later in the code to pull member values by member names, not index numbers. Can anyone set me straight? Thanks in advance ANIME AKBAR fucked around with this message at 03:20 on Nov 6, 2011 |
|
# ? Nov 6, 2011 03:06 |
|
|
# ? May 31, 2024 06:12 |
You could do something like this:code:
(I can't remember if you can actually do less-than comparisons on pointers like that. Use a different loop condition if you can't.)
|
|
# ? Nov 6, 2011 03:11 |
nielsm posted:You could do something like this: Hmm, that's actually pretty close to what I was trying. At least yours compiles, which is more than I can say for my own attempts. It's interesting that you define *fieldptr as a char instead of as the structure type it's pointing to... maybe that's the key. However I can't really confirm it works... my programming environment can only debug code by actually programming the hardware, then stepping through the code and reading the actual registers. And the compiler always optimizes away all the pointers somehow so I can't see their values... is there a decent, free compiler/debugger that will allow me to try out little snippets of code like this one?
|
|
# ? Nov 6, 2011 03:41 |
|
Another way to get around this is by using an array and then defining the friendly names for the indexes. You'll probably find examples of something like this in your microcontroller headers for memory mapped hardware addresses. i.e. PORTB will be defined as it's address in memory. I saw some code that used an enum for this which was neat as the author threw in an extra enum value that functioned as the size of the array:code:
|
# ? Nov 6, 2011 03:58 |
You could also define your struct with an inner union. Technically the behaviour of this is undefined but in practice it tends to do exactly what you expect.code:
|
|
# ? Nov 6, 2011 04:03 |
|
ANIME AKBAR posted:It's interesting that you define *fieldptr as a char instead of as the structure type it's pointing to Now, (void*)&thestruct == (void*)&thestruct.a, but since you took the address of its first member, you should still consider the variable to be pointing to the member, not the struct itself.
|
# ? Nov 6, 2011 12:27 |
|
ANIME AKBAR posted:However I can't really confirm it works... my programming environment can only debug code by actually programming the hardware, then stepping through the code and reading the actual registers. And the compiler always optimizes away all the pointers somehow so I can't see their values... is there a decent, free compiler/debugger that will allow me to try out little snippets of code like this one? Can you get the asm output? I've been doing this exact thing on an MSP430 and just examining the GCC assembly output to make sure I didn't do something wrong with unions since I haven't used them in years. I'd expect to see the base address of the union or struct or whatever loaded into a register and then writes to offsets from that register in the data size you're using. code:
code:
You might also want to haunt the embedded programming nanothread and the C/C++ megathread. csammis fucked around with this message at 17:07 on Nov 6, 2011 |
# ? Nov 6, 2011 17:05 |
|
Just so I'm clear - there isn't any portable way to do what he's asking, right? I'm sure there are plenty of things that will work, but there's nothing that must work?
|
# ? Nov 7, 2011 01:38 |
|
Nope. I think there's a guarantee that struct members are located in memory in the order they're declared, but there's nothing said about any padding between members.
|
# ? Nov 7, 2011 01:42 |
pseudorandom name posted:Nope. I think there's a guarantee that struct members are located in memory in the order they're declared, but there's nothing said about any padding between members. Well, since there is also a guarantee that every bit in memory can be accessed through a char, casting the a pointer to the first member to a char pointer and then reading sizeof(thestruct) chars forward you will also access all of the memory the struct occupies, of course that includes any padding inserted. Is this even relevant to anything? I forgot.
|
|
# ? Nov 7, 2011 01:55 |
|
GrumpyDoctor posted:Just so I'm clear - there isn't any portable way to do what he's asking, right? I'm sure there are plenty of things that will work, but there's nothing that must work? Well there's some dumb options: code:
|
# ? Nov 7, 2011 05:40 |
|
GrumpyDoctor posted:Just so I'm clear - there isn't any portable way to do what he's asking, right? I'm sure there are plenty of things that will work, but there's nothing that must work? The essence of microcontroller programming
|
# ? Nov 7, 2011 18:41 |
|
If you don't have any requirements for portability, you already have to document the hell out of your code for future generations to understand it, performance/size are at a premium and you're bending over backwards to deal with the abstractions of C, why don't you just write assembly?
|
# ? Nov 7, 2011 20:05 |
|
Internet Janitor posted:If you don't have any requirements for portability, you already have to document the hell out of your code for future generations to understand it, performance/size are at a premium and you're bending over backwards to deal with the abstractions of C, why don't you just write assembly? If "crazy C tricks" constitute 0.1% of the code then it is still worth learning C for the other 99.9% that doesn't have to be written in assembly.
|
# ? Nov 7, 2011 23:19 |
|
There's always inline assembly, or linking against object files originating from assembly.
|
# ? Nov 7, 2011 23:29 |
|
A question about POSIX thread-programming: I have a application where a thread (A) spawns two children-threads (B and C). Then i want to cancel thread C from thread B. I set cancel state in thread C using: pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL); Then i want to cancel the thread using: pthread_cancel(idC); I know that using Cancel is bad practice, but that is not really important. The problem is that when i call cancel from thread B thread C will not cancel, even though i have set several cancellation points using testcancel. If i call cancel from thread A thread C cancels emediatealy, when calling from thread B nothing happens.
|
# ? Nov 8, 2011 08:26 |
|
Are you sure the children threads are running in parallel and that the canceltype and cancelstate in C are set before B calls cancel?
|
# ? Nov 9, 2011 00:41 |
|
If a web server returns:code:
|
# ? Nov 9, 2011 12:46 |
|
ryo posted:If a web server returns: You can almost always cache unless the server explicitly tells you not to (usually this will be for privacy reasons). However, what you should do in this case is next time you display the content to the user you should send a request for the data with an If-Not-Modified-Since header with the value from the Date header you received. If you get a 304 in response you can use the cached value.
|
# ? Nov 9, 2011 13:02 |
|
Zombywuf posted:You can almost always cache unless the server explicitly tells you not to (usually this will be for privacy reasons). However, what you should do in this case is next time you display the content to the user you should send a request for the data with an If-Not-Modified-Since header with the value from the Date header you received. If you get a 304 in response you can use the cached value. Unfortunately it's still giving me 200 status :/ I know that the document hasn't changed. Perhaps I should continue in the Python thread? quote:>>> h = httplib2.Http('.cache')
|
# ? Nov 9, 2011 15:43 |
|
ryo posted:Unfortunately it's still giving me 200 status :/ Sorry, that should be "If-Modified-Since". If the server returns 200 it means one of two things: 1) The resource has changed. 2) The server does not support If-Modified-Since. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html Section 14.25 for reference.
|
# ? Nov 9, 2011 16:36 |
|
Thanks, I tried with If-Modified-Since and it still returned status 200. I'll contact the people that run the webserver to see what they say about it.
|
# ? Nov 9, 2011 16:53 |
|
I am coding html and I have a regular text link that I want to open a seperate page in a presized new window. That is not the problem, my problem is it's opening the new window in an obscure position on the screen and I would rather the new window open up in the center of the screen. I have seen countless javascript solutions to this problem but I want to avoid java at all costs if I can. If html/css cannot handle this on it's own perhaps there is a php solution availible? Here is what's there now: <body link="#FC6767" vlink="#FFF2AD" alink="#336699"> <div id="login"><a href="cwerklogin.html" target="popup" onclick="window.open('cwerklogin.html', 'popup', 'width=850,height=400'); return false" title="login">Login / Register > it's free!</a></div>
|
# ? Nov 9, 2011 20:24 |
Mindisgone posted:I am coding html and I have a regular text link that I want to open a seperate page in a presized new window. That is not the problem, my problem is it's opening the new window in an obscure position on the screen and I would rather the new window open up in the center of the screen. I have seen countless javascript solutions to this problem but I want to avoid java at all costs if I can. If html/css cannot handle this on it's own perhaps there is a php solution availible? Here is what's there now: "I don't wanna use Javascript!" *goes ahead and uses javascript* See that part with onclick=""? That's Javascript in the quotes. If you want to size and position browser windows like that you have to use client side scripting. A PHP script can't interactively control what happens on the user's screen, it can just send HTML* to the client. I'm pretty sure I would dislike getting a new window for logging in, but it probably depends on what exactly you want to do. Either way, you should probably change the target="popup" to target="_blank" to get a new, unnamed browser window, instead of using a window with the name "popup". *PHP can send any kind of data to the client, but the client has to send a request to the server first.
|
|
# ? Nov 9, 2011 21:31 |
|
Also Java != Javascript.
|
# ? Nov 9, 2011 21:44 |
|
It's not that I dislike java I'm just really inexperianced and will only start learning it after I pick up mysql but thank you for clearing that up I will straighten this out and add the necessary javascript. edit: I think it's worth mentioning, in case popular opinion is against me on this cause I'm open for suggestion, I want the main page to stay up the way it is so when people click the login link and login in the popup closes and the main page changes only the slightest to let the user know they're logged in and keep the focus on the main page. Maybe this is me just being too lazy to create a seperate page but it seems to flow well enough. Mindisgone fucked around with this message at 22:16 on Nov 9, 2011 |
# ? Nov 9, 2011 22:10 |
|
Java and JavaScript are completely different languages. They have different styles, different origins, and different places in the serving-content-to-users process.
|
# ? Nov 9, 2011 22:31 |
|
I didn't feel like writing javascript sorry for being lazy
|
# ? Nov 9, 2011 22:52 |
Mindisgone posted:I didn't feel like writing javascript sorry for being lazy Anyway, consider what purpose you are serving by opening a new window for the user to enter his login, as opposed to just sending him to a new page, having a login box visible on every page, or having one made visible on the current page when the user clicks a link/button. Consider that if you open a new window for getting login credentials you will either have to keep opening new pages after the login has been processed in that window, or you will have to somehow transfer "control" back to the original window. Consider popup-blockers, some people still use very aggressive ones. Consider that users (still) get confused by new windows popping up on top of others. I would suggest having your login simply be a separate page linked to without opening new windows or having dynamic flyouts or such, but make sure you include a "return address" functionality: A way to pass an URL the user should be redirected back to after logging in. That can be surprisingly useful.
|
|
# ? Nov 9, 2011 23:50 |
|
How do I edit the tab complete behavior in a shell? Specifically bash on OS X? I'm used to this behavior: -single tab on no input matches anything in the cwd. If the cwd has only 1 item it will insert that. If it has many items with a common prefix it will insert the prefix. -single tab on any amount of input matches and auto fills anything in PATH and cwd regardless of if its executable or not Shaocaholica fucked around with this message at 00:53 on Nov 10, 2011 |
# ? Nov 10, 2011 00:51 |
|
Mindisgone posted:I didn't feel like writing javascript sorry for being lazy I've actually been using floating DIVs for this kind of thing with some success. The secret is to only trigger on a user event; user clicks on something, they expect something to happen. Don't link it to onload or a timer or something like that because you'll just freak people out. Check out the discussion here: http://stackoverflow.com/questions/345987/how-to-create-a-javascript-floating-div-signup-form On the lighter side, I ran into a nasty one today. You visit the page, are looking around. About 30 seconds later a livechat window opens up and a 'representative' starts asking you questions. The best part? There's no button to close the live chat window, so it just sits there with its questions unanswered... haunting. Intrusive as hell and probably pretty scary to the uninformed user.
|
# ? Nov 10, 2011 04:47 |
|
Are there still legit uses for exceptions outside of the normal error reporting side of things? I just walked away from a programmer fight over something like this:code:
The worst part to me is it's C#, where you could just use the "is" operator and move on. I found later some of the background behind the wacky arguments was that some of them were told it was legit--or at least apropos to the task at hand--to use exceptions in some odd cases that weren't necessarily related to error handling. I don't get it myself, and I can't think of any examples. It also doesn't acknowledge why they just blindly take the exception, when foo.bar() just as well could have a problem. Let's not go there. It was a fun day.
|
# ? Nov 11, 2011 16:27 |
|
You know, maybe that code was once more sane, but profiling indicated that it was a performance bottleneck and it was rewritten to this less idiomatic but faster version. However, since that's almost certainly not what happened, that is indeed a coding horror in C#. (In general, what exceptions are used for depends a lot on the language, and to some extent on the libraries themselves. Python, for instance, uses exceptions to indicate when to stop enumerating something. In Apple's Objective-C code, exceptions are not for error reporting but for unrecoverable errors (yes, you're really not supposed to catch them).)
|
# ? Nov 11, 2011 16:56 |
|
pokeyman posted:You know, maybe that code was once more sane, but profiling indicated that it was a performance bottleneck and it was rewritten to this less idiomatic but faster version. Now I have to look up that Python exception handling style because that sounds kind of goofy to me.
|
# ? Nov 11, 2011 17:51 |
|
C# even supports as so you only need to perform the "probing" once. Good: code:
code:
code:
|
# ? Nov 11, 2011 18:10 |
|
Rocko Bonaparte posted:Now I have to look up that Python exception handling style because that sounds kind of goofy to me. It uses them for error handling too of course. It's just that when you care a bit less about performance, exceptions become convenient to use for other things and you can afford to take a bit more of a "try and see" approach. Sounds a bit weird, but it often ends up cleaner in the long run - you're doing what you want to do first then dealing with potential problems afterwards.
|
# ? Nov 11, 2011 19:00 |
|
Sedro posted:C# even supports as so you only need to perform the "probing" once. I do actually like the crazy method there for being the easiest on the eyes, but for it to be best overall, I would have to assume that it can optimize the two lookups. You'd think that would be something it could do at the byte code level, before the runtime even gets its mittens on it. I don't know if that happens though.
|
# ? Nov 11, 2011 19:06 |
|
A buddy of mine is creating a .bat and is a hard time with a particular aspect. He is writing a DOS test which queries the total disk space of a particular drive letter, and then runs a series of reads / writes / and compares until the disk is completely full. The problem he's having is querying the disk space and then converting that output into a manageable number which can them be put back into the .bat For instance, lets say you had a 1TB disk "E". From dos: E > "Dir" which would return "1099511627776 Bytes Free" Assuming the disk was empty. What he needs to do is then take that output, convert it to TB's and then be able to plug that result back into a the batch. He is claiming that the byte integer is just too large to work with... If anyone has any suggestions on simple ways we could go about this I'd really appreciate it. We were thinking of some sort of exe that could do the calculation and then created an output which can be called by dos... but we're not sure if that's the way to go.
|
# ? Nov 11, 2011 19:14 |
|
You should use a real scripting language, like Python or Ruby. Or Powershell.
|
# ? Nov 11, 2011 19:59 |
|
|
# ? May 31, 2024 06:12 |
|
TasteMyHouse posted:You should use a real scripting language, like Python or Ruby. Or Powershell. The actually test has been in place for some time as a .bat, so we're just trying to add some functionality to it without having to reinvent the wheel.
|
# ? Nov 11, 2011 20:04 |