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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Declaring char array problem

Status
Not open for further replies.

Sherak

Programmer
Sep 1, 2003
83
GB
I have a class in which a declare a series of arrays of chars and I am getting a real stinker problem. for example I declare the arrays like this

char Temp1[5];
char Temp2[5];
char Temp3[5];

Or

char Temp1[5], Temp2[5], Temp3[5];

Anyway the program is taking the first array as a array of 15 elements, the second as a array or 10 and the third as a array of 5, so I can write to Temp3[0] by doing either:

Temp3[0] = 'x';
or
Temp1[10] = 'x';

This is doing my head in big time as if I so a strcpy(Temp3, Temp1) I get a access overun, as it is trying to put 15 chars into a 5 char array!!!!... If I declare the arrays locally it doesnt occur just if I declare them in the class declaration.

Im stumped and half about to smash up my monitor as I can find no logical explanation for this.

Even before I put anything in the arrays, just declare them, If I step through to the first line of code and put watched on the arrays, it shows the last with 5 random elements the second with 10 (the last five being from the the last array) and the first with 15(the last 10 being form the second and last). Its as if it isnt putting in the end of string marker or somthing. I have even declared a single array at the end, of 10 elements and it gives me a array of 12 elements...... Please help.....please please, I told my bos it would only take me half a day to write this small program and now Im stuck on what should be beginers stuff :-S

thanks in advance
 
> Its as if it isnt putting in the end of string marker or somthing.

Exactly, You need to put the 'end of string marker' in. i.e.
Code:
  Temp1[0] = 'x'; 
  Temp1[1] = '\0';

Or alternatively, initalize all elements of your arrays to 0 first, such as:

memset(Temp1,0,sizeof(Temp1));

Temp1 is simply a pointer to the beginning of the first character array, and since they are all consecutive, Temp1[10] is a pointer into the the Temp3 char array.

Just make sure that your char arrays are 1 char longer than you need to accomadate the '\0'.

The debugger may still show all 15 chars when you inspect Temp1, but when you use it it in your code it should work correctly.

Good Luck.
 
The memory addresses in the class are chosen at compile time, and are more than likely immediately next to each other. Since you (I assume) didn't have the null characters at the end of your strings, the debugger just starts displaying at Temp1's address, everything until it hits a NULL character, as itsgsd pointed out. Using strcpy won't add the null character for you. You must do it manually.

Good luck,
Chris
 
I beg to differ

char buff [20];
strcpy (buff, "test line");
Edit1->Text = strlen (buff);

this returns a 9 into the edit.

****************************************************
copied from the help files.
****************************************************
Header File

string.h

Category

Inline Routines, Manipulation Routines

Syntax

#include <string.h>
char *strcpy(char *dest, const char *src);
wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);

#include <mbstring.h>
unsigned char *_mbscpy(unsigned char *dest, const unsigned char *src);

Description

Copies one string into another.

Copies string src to dest, stopping after the terminating null character has been moved.

Return Value

strcpy returns dest


I could be wrong though ;-)
tomcruz.net
 
butthead: What supernat03 meant is that if you have a string that DOESN'T have a nul terminator, you can't use strcpy() on it reliably.

In C, any string declared with double quotes automatically has a nul terminator, so your example is not valid.

Sherak: I'd listen to what itsgsd said. You always need to terminate your strings.

If you don't want to terminate your strings, you can always use memcpy(temp1,temp3,5) but watch out if you actually want to use them as string. You'll need to terminate them first.
 
Correct Vorlath, my sentences ran together. I wrote strcpy won't ADD a null character, but it doesn't read well. I simply meant strcpy won't add the termination for you based on the length of the destination set in the code by the programmer.

i.e.
char Buffer1[5];
char Buffer2[10];

strcpy(Buffer1, Buffer2);

This will overrun Buffer1. It won't truncate Buffer1 at byte 5 and insert a null character just because it's defined as such. That's a better explanation I hope!

Also, you said when you declare them locally, it doesn't occur. I think you're getting lucky. There just happens to be 0's in the memory at that time.

In short, always terminate your strings. :)

Thanks,
Chris
 
Thanks everyone, and yeah I could kick myself, instead of wondering why it wasnt putting the marker in I should have just put it in myself... thx

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top