Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with strcpy

Status
Not open for further replies.

kopo85

Programmer
Nov 29, 2006
3
US
I was just wondering if you could see anything wrong with this code off had that wouldn't make it work

char mid_store[6];
char output[512];
unsigned char sample[16][9];
int j;
int i;
int str_length = 0;

for(j = 0; j<9; j++)
{
for(j = 0; j<16; j++)
{
sample[j] = read_adc(0);
itoa(sample[j], mid_store);
strcpy(output, mid_store);
strcpy(output, ',');
}
for(i = 0; i < 16; i++)
{
if(sample[j]<10)
{str_length = str_length + 3;}
else if(sample[j]<100&&sample[j]>9)
{str_length = str_length + 4;}
else
{str_length = str_length + 5;}
}
}

printf("W 1 %d\r", str_length);
printf("%s", output);
 
1. Please use the [code][/code] tags when posting code

2. unsigned char sample[16][9];
But your for loops treat it as [9][16]

Or they would do, if not for
Code:
for(j = 0; j<9; j++)
for(j = 0; j<16; j++)
using the same subscript for both loops.
Your i subscript remains uninitialised, and probably accessing WAY outside of your array.

> strcpy(output, ',');
Do you get any warnings with this - you should do. strcpy copies strings, not characters.
So it would be [tt]strcpy(output, ",");[/tt]

Additionally, you probably want all these to be strcat() calls if you intend to build up a large line.
start with
[tt]output[0] = '\0';[/tt]
then use strcat()

--
 
Sorry, the second for statement is actually suppose to be
Code:
for([b]i[/b] = 0; [b]i[/b]<16; [b]i[/b]++)
{
sample[i][j] = read_adc(0);
itoa(sample[i][j], mid_store);
strcpy(output, mid_store);
strcpy(output, ',');
}

I guess I made another mistake in my code when I posted it on this forum also.
Code:
for(i = 0; i < 16; i++)
{       
if(sample[i][j]<10)
{str_length = str_length + [b]2[/b];}                
else if(sample[i][j]<100&&sample[i][j]>9)
{str_length = str_length + [b]3[/b];}
else
{str_length = str_length + [b]4[/b];}
}

Taking this into account, I don't see how I would be writing past my array, I have a array of 512, and would write at max
512 (16*8*4) = 512

To get this I should probably make the first loop
Code:
for(j = 0; j<[b]8[/b]; j++)

I haven't run into any problems using the strcpy(output,','), but will try strcpy(output, ",").

I really dont' think this is the problem, because when I tried to write the first 5 characters, the information written was completely incorrect and lacked a comma.

I'll look more into strcat(), this may be my whole problem. Thank you very much, and if anyone else has any comments to add, it would still be appreciated
 
Sorry, actually it should be
Code:
for(j=0;j<9;j++)
that would make my equation correct. This forum could really use an edit button.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top