class arrays

I'm writing my own calculator program and it's going to kick ass.
I've defined a class called 'cterm' in order to make assigning values and signs and operations to each term in the input problem a little easier. Like this:
1
2
3
4
5
class cterm{
protected:
	double dvalue;
	char indicator[3];
	int nlocation;

dvalue is the numerical value of the term. nlocation is the location that the term appears in the input string (102+4*9.2;the cterm instance that has 102 would have an nlocation of 0, and the instance of 9.2 would have an nlocation of 2).
The indicator array is what I'm going to use to store all non-numerical characters in the input array (+,-,*,/,(,)), and the indicator array of any cterm instance will have every operator/sign that is immediately to the left of the number.
(102+4*9.2;the cterm instance for 9.2 would have an indicator array of '*__',4 would have '+__')

The problem i'm having occurs when I declare an array of cterm objects at the beginning of my program.

//main.cpp
1
2
3
4
5
6
7
8
9
cterm TERMS[MAX];
int counter=0;
cterm* termpnt=&(TERMS[0]);
while(counter<=MAX){
        termpnt=&(TERMS[counter]);
	termpnt->setOP(input);
	termpnt->setTerm(input);
	counter++;
}

Only the 1st line of code here is important, but i posted the rest for context.
MAX is defined as 10. I have explicitly defined the default constructor (which is called 10 times at the line 'cterm TERMS[MAX];') as...
1
2
3
4
5
6
7
8
9
cterm():
dvalue(0),nlocation(-1){
	int counter=0;
	while(counter<=2){
		indicator[counter]='a';
		counter++;
	}
	std::cout << indicator << std::endl;
}

Here, the while loop is simply present in order to default unassigned elements to 'a,' which, to me, seems preferable to having them contain junk values.

tldr;
Why does the std::cout << indicator << std::endl; line print this...
http://imageshack.us/photo/my-images/829/cppforumprob.jpg/

Thanks for reading. It's a long one I know, and I apologize if my description wasn't sufficient. Let me know if it is lacking and I'll explain in more detail.
1
2
3
4
5
6
7
8
9
cterm TERMS[MAX];
int counter=0;
cterm* termpnt=&(TERMS[0]);
while(counter<=MAX){
        termpnt=&(TERMS[counter]);
	termpnt->setOP(input);
	termpnt->setTerm(input);
	counter++;
}


Valid indices for TERMS include 0 to MAX-1. Your loop executes when counter is equal to MAX, causing undefined behavior. I'm not sure what you hope to accomplish with the termpnt variable. It isn't needed:

1
2
3
4
5
6
7
    cterm TERMS[MAX];
    int counter=0;
    while ( counter<MAX )
    {
        TERMS[counter].setOP(input) ;
        TERMS[counter++].setTerm(input) ;
    }



In

1
2
3
4
5
6
7
8
9
cterm():
dvalue(0),nlocation(-1){
	int counter=0;
	while(counter<=2){
		indicator[counter]='a';
		counter++;
	}
	std::cout << indicator << std::endl;
}


It would appear you intended to interpret indicator as a c-string. c-strings are nul-terminated. indicator is not.



It would appear you intended to interpret indicator as a c-string. c-strings are nul-terminated. indicator is not.

Why not? indicator is defined like...
1
2
3
4
class cterm{
protected:
	double dvalue;
	char indicator[3];
Why not? indicator is defined like..


What does indicator's definition have to do with whether it is nul-terminated?

indicator[0], indicator[1] and indicator[2], the only elements of indicator are all set to 'a' after the loop in the constructor. Where is the nul character with which they are terminated?
Last edited on
If it is defined as a c-string,
char indicator[3];
then shouldn't it be null terminated? And when
std::cout << indicator << std::endl;
is reached, should it then print only "aaa" and then stop, because the null-terminator was reached?
If it is defined as a c-string,
char indicator[3];
then shouldn't it be null terminated?

It is not defined as a c-string. It is defined as an array of 3 char. There is no such thing as a c-string type. A c-string is simply an array of char that is treated according to certain conventions. One of those conventions is that a c-string is terminated by a nul character. You do not abide by this convention, therefore indicator may not be treated as a c-string.

I'm not sure why you're having difficulty with this. Surely you can look at the results of your code execution and see the evidence of what I've said.


should it then print only "aaa" and then stop, because the null-terminator was reached?

Once again, there is no nul character terminating indicator. indicator has room for exactly three characters. You've assigned each element the value of 'a'.
Last edited on
I see. When is it that the compiler automatically appends the '\0' character to the end of the string?
char indicator[]="foo"
?
A string literal, "foo" includes the terminating nul character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

void print(char*array, unsigned size)
{
    std::cout << "{ " ;
    for ( unsigned i=0; i<size; ++i )
    {
         unsigned char ch = array[i] ;

         if ( ch < 32 )
            std::cout << "'\\" << unsigned(ch) << "' " ;
         else
             std::cout << ch << ' ' ;
    }
    std::cout << "}\n" ;
}


int main()
{
    std::cout << "sizeof(\"foo\"): " << sizeof("foo") << '\n' ;

    char indicator[] = "foo" ;

    std::cout << "sizeof(indicator): " << sizeof(indicator) << '\n' ;

    std::cout << "\nprint(\"foo\", sizeof(\"foo\"))\n" ; 
    print("foo", sizeof("foo")) ;

    std::cout << "\nprint(indicator, sizeof(indicator))\n" ;
    print(indicator, sizeof(indicator)) ;
}
sizeof("foo"): 4
sizeof(indicator): 4

print("foo", sizeof("foo"))
{ f o o '\0' }

print(indicator, sizeof(indicator))
{ f o o '\0' }
I see. Thanks for your patience guys!
Topic archived. No new replies allowed.