C Programming Help Required

Serious non-car discussion - computers, films etc. Pointless posts will be removed.

C Programming Help Required

Postby sailorbob on Tue Nov 04, 2008 8:29 pm

Can anyone help with the following problem? I am trying to fix a problem in some code and, being a complete novice with no C programming experience I am probably missing the obvious. Below is the section that is causing the problem; the code compiles OK but when you run the program it is baulking at the for statement and skipping the rest of the code.

:
/* output the predefined special function registers                                      */
    sp = predef_sfr;
    for(i = 0; i < sizeof(predef_sfr) / sizeof(PREDEF_SFR); ++i)
    {   memset(outp, ' ', 79);
        if((sts = format_addr20(sp->sfr_addr, &outp[COL_PC])) < 0) /* put value in 1st 5 chars */
            return(sts);
        memcpy(&outp[20], sp->sfr_name, strlen(sp->sfr_name) );
        memcpy(&outp[54], "equ", 3);                            /* put SFR name in output    */
        outp[60] = '$';
        if((sts = format_addr20(sp->sfr_addr, &outp[61])) < 0)
            return(sts);
        outp[66] = '\0';                                     /* pull null termination in output */
        output_line(file_ptr_output, showhex, spacer_line, outp);
        ++sp;
    }
I think it is the second expression that is the cause of the problem where the loop should be comparing the loop counter with the total length of a structure. I'm sure that more info will be required to help understand the code snippet so just ask and I'll try to answer.

Thanks in advance.
sailorbob
Elite Post Master
Elite Post Master
 
Posts: 1430
Joined: Sun Aug 10, 2003 10:52 am

Re: C Programming Help Required

Postby sjoyce666 on Tue Nov 04, 2008 10:26 pm

I think you need to have brackets like so

(sizeof(predef_sfr) / sizeof(PREDEF_SFR));

I think the test conditions are causing the problems as it is thinking you are only running the for statement when i is less than "sizeof(predef_sfr)" divided by "sizeof(PREDEF_SFR)" rather than what you really want

i < (sizeof(predef_sfr) / sizeof(PREDEF_SFR));

Let me know if this fixes it

Scott
sjoyce666
Elite Post Master
Elite Post Master
 
Posts: 2199
Joined: Fri Apr 19, 2002 11:58 am
Location: Yeovil, Somerset

Re: C Programming Help Required

Postby sailorbob on Tue Nov 04, 2008 11:15 pm

No joy I'm afraid. I think the compiler is 'smart' enough to deal with the arithmetical hierarchy. My suspicions are that it's to do with not getting my structures correct, but this is a guess.
sailorbob
Elite Post Master
Elite Post Master
 
Posts: 1430
Joined: Sun Aug 10, 2003 10:52 am

Re: C Programming Help Required

Postby andymac on Fri Nov 07, 2008 10:59 am

I'd be inclined to think there's a missing bracket, either before or after that code section.
Nothing to see here...
andymac
Elite Pie Master
Elite Pie Master
 
Posts: 11056
Joined: Mon Jun 11, 2001 1:00 am
Your car: Your car: Your car: Your car:

Re: C Programming Help Required

Postby sailorbob on Fri Nov 07, 2008 11:30 am

I have tried pretty much every combination I could think of without any success. The compiler rejects most stupid errors it finds. Here's a another (simpler) section of code from the same program that suffers the same problem:
:
int     load_predef_sfr(PREDEF_SFR *predef_sfr)
{   int         i;                   /* loop counter variable */
    int         sts;                 
    PREDEF_SFR  *sp;                 
    sp = predef_sfr;                 /* assign 'sp' pointer to match 'predef_sfr' pointer */
   
    for(i = 0; i < sizeof(predef_sfr) / sizeof(PREDEF_SFR); ++i)   /* this line isn't working */
    {   
        if((sts = add_name(sp->sfr_name, sp->sfr_addr)) < 0)
            return(sts);                                       
        ++sp;                                       /* increment pointer */
    }
    return(0);
}

Keep the suggestions coming thanks :)
sailorbob
Elite Post Master
Elite Post Master
 
Posts: 1430
Joined: Sun Aug 10, 2003 10:52 am

Re: C Programming Help Required

Postby sjoyce666 on Fri Nov 07, 2008 4:06 pm

for(i = 0; i < sizeof(predef_sfr) / sizeof(PREDEF_SFR); ++i) /* this line isn't working */

im sure it is the line above, the compiler wont be clever enough to know you want to check that "i" is less than "sizeof(predef_sfr) / sizeof(PREDEF_SFR)"

the compiler will look at that line and think it needs to do "i < sizeof(predef_sfr)" divided by "sizeof(PREDEF_SFR)"

i still think the line that you've marked as failing should be

for(i = 0; i < (sizeof(predef_sfr) / sizeof(PREDEF_SFR)); ++i)

It seems strange that the same line of code is failing and it appears in both areas you are having a problem with

Scott
sjoyce666
Elite Post Master
Elite Post Master
 
Posts: 2199
Joined: Fri Apr 19, 2002 11:58 am
Location: Yeovil, Somerset

Re: C Programming Help Required

Postby sailorbob on Fri Nov 07, 2008 5:18 pm

I tried for(i = 0; i < (sizeof(predef_sfr) / sizeof(PREDEF_SFR)); ++i) again and it still skips the entirety of the code up to the second return. This convinces me even more that the compiler is correctly performing mathematical operations before any logical expressions.

Part of my lack of understanding the problem is down to not being able to find suitable examples of how sizeof is used with structures. Everything seems to be to do with when it's used in conjunction with malloc which is different from what I'm trying to achieve.

Is there a simpler way of checking whether you have worked your way through a structure?
sailorbob
Elite Post Master
Elite Post Master
 
Posts: 1430
Joined: Sun Aug 10, 2003 10:52 am

Re: C Programming Help Required

Postby sjoyce666 on Fri Nov 07, 2008 7:09 pm

sailorbob :I tried for(i = 0; i < (sizeof(predef_sfr) / sizeof(PREDEF_SFR)); ++i) again and it still skips the entirety of the code up to the second return. This convinces me even more that the compiler is correctly performing mathematical operations before any logical expressions.

Part of my lack of understanding the problem is down to not being able to find suitable examples of how sizeof is used with structures. Everything seems to be to do with when it's used in conjunction with malloc which is different from what I'm trying to achieve.

Is there a simpler way of checking whether you have worked your way through a structure?


No idea i'm afraid, im not a C programmer and have never done any C programming but i am a programmer so logically it all looks fine, probably just down to a C syntax problem which i cant help you with im afraid

Might be an idea to post this on a dedicated programmers forum or experts exchange as they are quite helpful over there

Scott
sjoyce666
Elite Post Master
Elite Post Master
 
Posts: 2199
Joined: Fri Apr 19, 2002 11:58 am
Location: Yeovil, Somerset

Re: C Programming Help Required

Postby sailorbob on Mon Nov 24, 2008 12:14 pm

Just to close this out, I have been told what the problem is; The code wasn't distinguishing between the array eecv_predef_syms[] (which would have a size of say 128) and the pointer that was passed in (which has a size of 4). When the integer division by sizeof(PREDEF_SYMS) was done the result was zero, so no trips around the for loop.
sailorbob
Elite Post Master
Elite Post Master
 
Posts: 1430
Joined: Sun Aug 10, 2003 10:52 am

Re: C Programming Help Required

Postby andymac on Mon Nov 24, 2008 3:04 pm

And the fix was....? :D :P
Nothing to see here...
andymac
Elite Pie Master
Elite Pie Master
 
Posts: 11056
Joined: Mon Jun 11, 2001 1:00 am
Your car: Your car: Your car: Your car:

Re: C Programming Help Required

Postby sailorbob on Mon Nov 24, 2008 4:39 pm

Adding a new variable that is set equal to the size of the structure (early on in the main routine) and using that in the loop rather the sizeof(predef_sfr). This means you have
:
for(i = 0; i < num_sfrs / sizeof(PREDEF_SFR); ++i)
rather than
:
for(i = 0; i < sizeof(predef_sfr) / sizeof(PREDEF_SFR); ++i)
sailorbob
Elite Post Master
Elite Post Master
 
Posts: 1430
Joined: Sun Aug 10, 2003 10:52 am

Re: C Programming Help Required

Postby Ollie on Mon Nov 24, 2008 7:24 pm

For the sake of neatness and my own sanity, I tend to avoid using sums in the declaration of a loop anyway. Personally I'd declare a variable to contain the value of the sum and then use that instead.

ie.
:
aNumber = num_sfrs / sizeof(PREDEF_SFR)
for(i = 0; i < aNumber; ++i)
Image
Ollie
Elite Post Master
Elite Post Master
 
Posts: 11761
Joined: Thu Aug 08, 2002 8:38 am
Location: Northants
Your car: VW Polo SEL 1.6 TDI

Car: 1988 Ford Fiesta XR2

Re: C Programming Help Required

Postby sailorbob on Mon Nov 24, 2008 7:52 pm

You might be right but (a) it's someone else’s code I’m fixing/modifying, and (b) I don’t know what I’m really doing when it come to C :lol:
sailorbob
Elite Post Master
Elite Post Master
 
Posts: 1430
Joined: Sun Aug 10, 2003 10:52 am