C Programming Help Required
13 posts • Page 1 of 1
C Programming Help Required
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.
Thanks in advance.
- :
/* 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;
}
Thanks in advance.
- sailorbob
- Elite Post Master

- Posts: 1430
- Joined: Sun Aug 10, 2003 10:52 am
Re: C Programming Help Required
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
(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

- Posts: 2199
- Joined: Fri Apr 19, 2002 11:58 am
- Location: Yeovil, Somerset
Re: C Programming Help Required
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

- Posts: 1430
- Joined: Sun Aug 10, 2003 10:52 am
Re: C Programming Help Required
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

- Posts: 11056
- Joined: Mon Jun 11, 2001 1:00 am
- Your car: Your car: Your car: Your car:
Re: C Programming Help Required
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:
Keep the suggestions coming thanks
- :
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

- Posts: 1430
- Joined: Sun Aug 10, 2003 10:52 am
Re: C Programming Help Required
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
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

- Posts: 2199
- Joined: Fri Apr 19, 2002 11:58 am
- Location: Yeovil, Somerset
Re: C Programming Help Required
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?
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

- Posts: 1430
- Joined: Sun Aug 10, 2003 10:52 am
Re: C Programming Help Required
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

- Posts: 2199
- Joined: Fri Apr 19, 2002 11:58 am
- Location: Yeovil, Somerset
Re: C Programming Help Required
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

- Posts: 1430
- Joined: Sun Aug 10, 2003 10:52 am
- andymac
- 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
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 haverather than
- sailorbob
- Elite Post Master

- Posts: 1430
- Joined: Sun Aug 10, 2003 10:52 am
Re: C Programming Help Required
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.
ie.
- Ollie
- 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
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 
- sailorbob
- Elite Post Master

- Posts: 1430
- Joined: Sun Aug 10, 2003 10:52 am
13 posts • Page 1 of 1
