The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Program skipping elements in struct array
Discuss Program skipping elements in struct array in the C Programming forum on Dev Shed. Program skipping elements in struct array C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 31st, 2013, 02:26 AM
|
|
|
|
Program skipping elements in struct array
I have a program that I am using to print out a header that displays a message with items from a structure array. Now, the code I have in the main function seems to work, but I was trying to create a function to do the same thing. The function seems to work, but it always skips the second element and I can't figure out why. Nothing is being incremented other than what is in the loop and the variable is correct so it should be selecting the 2nd item, but it just skips over it for some reason.
I was wondering if someone might look at my code and see what I am doing wrong. Thanks.
Code:
// Struct-Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "conio.h"
#define MAMCR_OFF 0
#define MAMCR_PARTIAL 1
#define MAMCR_FULL 2
//#define MAMTIM_CYCLES (((CCLK)+19999999)/20000000)
#define MAMTIM_AUTOCFG 0
#define MAMTIM_1_CLK 1
#define MAMTIM_2_CLK 2
#define MAMTIM_3_CLK 3
#define MAMTIM_4_CLK 4
#define MAMTIM_5_CLK 5
#define MAMTIM_6_CLK 6
#define MAMTIM_7_CLK 7
#define MAMTIM_MAX_CLK 7
typedef struct scb2300params {
unsigned __int16 PLL_M_Mul; /* PLL Multiplier. Valid values 6 through 512*/
unsigned __int8 PLL_N_Div; /* PLL Divider. Valid values 1 through 32 */
unsigned __int32 PLL_Fcco; /* Frequency (Hz) of PLL output */
unsigned __int8 CCLK_Div; /* CPU Clock divider, cclk */
unsigned __int8 MAMMode; /* */
unsigned __int8 MAMTim;
} scb2300_t;
char stringbuffer[88];
/* M N Fcco CCLKDIV MAMCR MAMTIM CCLK */
/*
* TODO Fill in the rest of this table with the tests to perform.
*/
const scb2300_t ConfigMemTestDesc[] = {
{ 12, 1, 288000000, 24, MAMCR_OFF, MAMTIM_1_CLK /* MAMTIM=1, 12 MHz */ },
{ 12, 1, 288000000, 24, MAMCR_PARTIAL, MAMTIM_1_CLK /* MAMTIM=1, 12 MHz */ },
{ 12, 1, 288000000, 24, MAMCR_FULL, MAMTIM_1_CLK /* MAMTIM=1, 12 MHz */ },
{ 12, 1, 288000000, 12, MAMCR_OFF, MAMTIM_2_CLK /* MAMTIM=2, 24 MHz */ },
{ 12, 1, 288000000, 12, MAMCR_PARTIAL, MAMTIM_2_CLK /* MAMTIM=2, 24 MHz */ },
{ 12, 1, 288000000, 12, MAMCR_FULL, MAMTIM_2_CLK /* MAMTIM=2, 24 MHz */ },
{ 12, 1, 288000000, 10, MAMCR_OFF, MAMTIM_2_CLK /* MAMTIM=2, 28.8 MHz */ },
{ 12, 1, 288000000, 10, MAMCR_PARTIAL, MAMTIM_2_CLK /* MAMTIM=2, 28.8 MHz */ },
{ 12, 1, 288000000, 10, MAMCR_FULL, MAMTIM_2_CLK /* MAMTIM=2, 28.8 MHz */ },
{ 12, 1, 288000000, 8, MAMCR_OFF, MAMTIM_2_CLK /* MAMTIM=2, 36 MHz */ },
{ 12, 1, 288000000, 8, MAMCR_PARTIAL, MAMTIM_2_CLK /* MAMTIM=2, 36 MHz */ },
{ 12, 1, 288000000, 8, MAMCR_FULL, MAMTIM_2_CLK /* MAMTIM=2, 36 MHz */ },
{ 12, 1, 288000000, 6, MAMCR_OFF, MAMTIM_3_CLK /* MAMTIM=2, 48 MHz */ },
{ 12, 1, 288000000, 6, MAMCR_PARTIAL, MAMTIM_3_CLK /* MAMTIM=3, 48 MHz */ },
{ 12, 1, 288000000, 6, MAMCR_FULL, MAMTIM_3_CLK /* MAMTIM=3, 48 MHz */ },
{ 12, 1, 288000000, 5, MAMCR_OFF, MAMTIM_4_CLK /* MAMTIM=4, 57.6 MHz */ },
{ 12, 1, 288000000, 5, MAMCR_PARTIAL, MAMTIM_4_CLK /* MAMTIM=4, 57.6 MHz */ },
{ 12, 1, 288000000, 5, MAMCR_FULL, MAMTIM_4_CLK /* MAMTIM=4, 57.6 MHz */ },
{ 15, 1, 360000000, 6, MAMCR_OFF, MAMTIM_4_CLK /* MAMTIM=4, 60 MHz */ },
{ 15, 1, 360000000, 6, MAMCR_PARTIAL, MAMTIM_4_CLK /* MAMTIM=4, 60 MHz */ },
{ 15, 1, 360000000, 6, MAMCR_FULL, MAMTIM_4_CLK /* MAMTIM=4, 60 MHz */ },
{ 12, 1, 288000000, 4, MAMCR_OFF, MAMTIM_4_CLK /* MAMTIM=3, 72 MHz */ },
{ 12, 1, 288000000, 4, MAMCR_PARTIAL, MAMTIM_4_CLK /* MAMTIM=2, 72 MHz */ },
{ 12, 1, 288000000, 4, MAMCR_FULL, MAMTIM_4_CLK /* MAMTIM=2, 72 MHz */ },
};
void printString(char *stringbuffer)
{
printf("%s",stringbuffer);
}
void print_Header(int i, const scb2300_t ConfigMemTestDesc[],char buffer[50])
{
int static j = 0;
//header
printString("\n\r\n\r===================================================================");
sprintf(buffer,"\n\r%luMHz Test #%u \n\r", ((ConfigMemTestDesc[i].PLL_Fcco/1000000)/ConfigMemTestDesc[i].CCLK_Div),j);
printString(buffer);
sprintf_s(buffer,50,"j:%d\n\r",j++);
printString(buffer);
sprintf_s(buffer,50,"CCLKDIV:%d ", ConfigMemTestDesc[i].CCLK_Div);
printString(buffer);
sprintf_s(buffer,50,"PLL_Fcco:%lu ", ConfigMemTestDesc[i].PLL_Fcco);
printString(buffer);
sprintf_s(buffer,50,"MAMMode:%d ", ConfigMemTestDesc[i].MAMMode);
printString(buffer);
sprintf_s(buffer,50,"MAMTim:%d ", ConfigMemTestDesc[i].MAMTim);
printString(buffer);
sprintf_s(buffer,50,"N:%u ", ConfigMemTestDesc[i].PLL_N_Div);
printString(buffer);
sprintf_s(buffer,50,"M:%u\n\r", ConfigMemTestDesc[i].PLL_M_Mul);
printString(buffer);
printString("--------------------------------------------------------------------\n\r");
if(j == 3)
j=0;
}
int _tmain(int argc, _TCHAR* argv[])
{
scb2300_t SCBParams;
/* Initial SCB Parameters */
SCBParams.PLL_M_Mul = 12; /* PLL Multiplier. Valid values 6 through 512*/
SCBParams.PLL_N_Div = 1; /* PLL Divider. Valid values 1 through 32 */
SCBParams.PLL_Fcco = 288000000; /* Frequency (Hz) of PLL output */
SCBParams.CCLK_Div = 6; /* CPU Clock divider, cclk */
SCBParams.MAMMode = MAMCR_PARTIAL; /* MAM mode Partial is the preferred setting for Rev -,A parts */
SCBParams.MAMTim = MAMTIM_AUTOCFG; /* Let initMAM calculate the optimal MAM timing */
int i = 0,k=0, j=1;
int ArraySize = sizeof(ConfigMemTestDesc)/sizeof(ConfigMemTestDesc[0]);
char buffer[50];
sprintf_s(stringbuffer,88, "\n\rProgramming Assignment #2: Built %s %s.\n\r\n\r",
__DATE__,
__TIME__);
printf("\n\r%s\n\r",stringbuffer);
/*TODO: loop through the ConfigMemTestDesc*/
//for(i = 0;i < ArraySize; i++)
for(i = 0;i < ArraySize; i++)
{
//TODO: Need to let the UART finish transmitting the last character
//* A. delay before clobbering it with initHardware() - simple and crude
//* B. or loop until the UART status bit says done - advanced
//sprintf(stringbuffer, "\n\r%d\n\r",i);
//printString(stringbuffer);
/* Delay for a bit */
for (k=0;k< 100000;k++)
;
// TODO: uncomment initHardware((scb2300_t *)&ConfigMemTestDesc[i]);
//initHardware((scb2300_t *)&ConfigMemTestDesc[i]);
// //header
// printString("\n\r\n\r===================================================================");
// sprintf(buffer,"\n\r%luMHz Test #%u \n\r", ((ConfigMemTestDesc[i].PLL_Fcco/1000000)/ConfigMemTestDesc[i].CCLK_Div),ConfigMemTestDesc[i].MAMMode+1);
// printString(buffer);
// sprintf(buffer,"CCLKDIV:%d ", ConfigMemTestDesc[i].CCLK_Div);
// printString(buffer);
//sprintf(buffer,"PLL_Fcco:%lu ", ConfigMemTestDesc[i].PLL_Fcco);
//printString(buffer);
//sprintf(buffer,"MAMMode:%d ", ConfigMemTestDesc[i].MAMMode);
//printString(buffer);
//sprintf(buffer,"MAMTim:%d ", ConfigMemTestDesc[i].MAMTim);
//printString(buffer);
//sprintf(buffer,"N:%u ", ConfigMemTestDesc[i].PLL_N_Div);
//printString(buffer);
//sprintf(buffer,"M:%u\n\r", ConfigMemTestDesc[i].PLL_M_Mul);
//printString(buffer);
//printString("--------------------------------------------------------------------\n\r");
print_Header(i,(scb2300_t *)&ConfigMemTestDesc[i] ,buffer);
//PerformReadTests();
}
//
// Everything is done.
//
sprintf_s(stringbuffer,88, "\n\rProgramming Assignment #2: Exiting.\n\r");
printString(stringbuffer);
getche();
return 0;
}
|

January 31st, 2013, 02:53 AM
|
 |
Contributed User
|
|
|
|
|
> print_Header(i,(scb2300_t *)&ConfigMemTestDesc[i] ,buffer);
You're doing the indexing TWICE.
In the function, you also reference ConfigMemTestDesc[i] as well.
So on the first iteration, you call it with
print_Header(0,(scb2300_t *)&ConfigMemTestDesc[0] ,buffer);
Second time,
print_Header(1,(scb2300_t *)&ConfigMemTestDesc[1] ,buffer);
which in the function, becomes ConfigMemTestDesc[2]
Then it's off into the weeds somewhere....
|

January 31st, 2013, 02:19 PM
|
|
|
Quote: | Originally Posted by salem > print_Header(i,(scb2300_t *)&ConfigMemTestDesc[i] ,buffer);
You're doing the indexing TWICE.
In the function, you also reference ConfigMemTestDesc[i] as well.
So on the first iteration, you call it with
print_Header(0,(scb2300_t *)&ConfigMemTestDesc[0] ,buffer);
Second time,
print_Header(1,(scb2300_t *)&ConfigMemTestDesc[1] ,buffer);
which in the function, becomes ConfigMemTestDesc[2]
Then it's off into the weeds somewhere.... |
Thanks, I think I know where I went wrong now. I must have been too tired last night to see it.
I replaced
Code:
print_Header(i,(scb2300_t *)&ConfigMemTestDesc[i] ,buffer);
with
Code:
print_Header(i,(scb2300_t *)&ConfigMemTestDesc ,buffer);
and now it works the way I intended.

|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|