CompuServe Thread

#Need C Pointer Help…

7 messages in this thread
#118777From: George GravesSep 2, 1990 7:44 PM
/* See line 35 below for question. */ struct MCS { char IString; char MString; char FString; char PSeparator; char PNumType; unsigned int Action; }; struct emulate { char TermDesc[31]; struct NUM NumDesc; struct BIN BoolDesc; unsigned int CtrlChar[32]; struct GFX Graphics; int MCS_Count; struct MCS StringDesc[256]; }; EvalControlString(struct emulate *Emulate, struct screen _far *Screen, struct state *State) { int i; if (!State->have_initial) { State->have_modifier = State->have_final = State->next_parm_number = 0; for (i = 0; i < Emulate->MCS_Count; i++) { /* How can I compare the single character at IString to the character */ /* at stringbufferlength past stringbuffer without using arrays? */ /* How do I code to address MString, FString …? */ if (Emulate->StringDesc[i].IString == *(State->stringbuffer + State->stringbufferlength)) { if (!(Emulate->StringDesc[i].MString || Emulate->StringDesc[i].FString || Emulate->StringDesc[i].PSeparator || Emulate->StringDesc[i].PNumType)) { PerformAction(Emulate->StringDesc[i].Action, Emulate, Screen, State); break; } else { State->have_initial = TRUE; } } } } else { } }
#118780From: Mike Spille/ManxSep 2, 1990 8:19 PM
Your code looks OK to me – what exactly is the problem? Are you getting a compiler error or does the code run incorrectly? From what I can see, you are in fact comparing the character at IString with the character at state->stringbuffer+state->stringbufferlenght. As for addressing Mstring, Fstring, etc. you already do that in your if-statement below the initial comparison. I guess a bit more detail regarding the nature of the error is what I need. -Mike
#118790From: George GravesSep 2, 1990 8:52 PM
The code works, but I'd like to change it from subscript-based to pointer-based addressing eliminating the [i]'s. I've tried several ways, but I don't get the hang of it. I'm just learning pointer processing and don't know how to use the pointer addressing to address the i'th occurrence of my array (if there is a way, which I'm pretty sure there is…) Thanks for taking a look at this for me.
#118800From: Mike Spille/ManxSep 2, 1990 10:22 PM
Basically, you can't (at least not in one line). The best optimization I could see for your code would be to say: struct MCS *temp; temp = &(Emulate->StringDesc[i]); if (temp->IString == *State->stringbuffer + State->stringbufferlength))) { if (temp->MString || temp->FString || temp->PSeperator || temp->PNumtype) … You get the idea – basically assign 'temp' to point to the MCS structure you want. BTW, that's one _monster_ of a structure that you have there – the struct MCS StringDesc[256] field alone takes 2K!! Anyway, hope this helps. -Mik e
#119007From: George GravesSep 4, 1990 8:02 AM
Thanks for the response… 1. Another question: Can we say temp = &(Emulate->StringDesc + i); and avoid the subscript evaluation? Although I'm not using the Manx compiler (and not writing for the Amiga) from what I've seen, most compilers do a much better job with pointer arithmetic than with subscript evaluation, even if it's nearly the same (e.g. only a variable, not an expression to evaluate.) 2. When I see what the "real world" requires, MCS will be smaller, now I'm struggling to get code to work and run fast. Thanks again for your help/interest. George
#119035From: Mike Spille/ManxSep 4, 1990 4:13 PM
In general, no compiler is going to show a difference in code generation in what you've shown in 1) compared to what I did. _Both_ versions are basically doing array indexing – mine just happens to do it using [i], and yours does +i. Both constructs do the same thing, they just look different. -Mike
#119052From: George GravesSep 4, 1990 9:15 PM
MSC Version 6.00 on the PC with optimization handles them very differently. It takes about 6 additional instructions to do the array indexing over the pointer based addressing with that compiler, and since I'm attempting to write a multi-term terminal emulator, on a lookup like that, it's going to be important. BTW, I am using this forum because I _AM_ an Amiga user and the folks here seem a lot friendlier and there aren't so many brain-damaged programmers with bad info on this forum. Thanks again for the assistance.