Hi,
Just can't figure this out despite having spent hours reading about pointer etc.
Is it possible to pass a char* to a function and have the length checked at compile time?
With a structue I can do this
1 2 3 4 5 6 7 8 9
typedefchar t_mystring[3];
typedefstruct t_mystruct {
t_mystring str;
};
t_mystruct s1 = {"f"}; // Ok
t_mystruct s2 = {"fo"}; // Ok
t_mystruct s3 = {"foo"}; // Fails as expected - perfect!
// s3 line give compiler "error: initializer-string for array of chars is too long"
Now, since I a want some functionallity associated to the structure I'd like to make a class to which I pass the string to the constructor. Compare below.
1 2 3 4 5 6 7 8 9 10 11 12 13
typedefchar t_mystring[3];
class MyClass {
t_mystring s;
public :
MyClass(t_mystring initstr) {
s = initstr; // Fails with;
// error: incompatible types in assignment of 'char*' to 'char [3]'
};
};
MyClass obj1 = MyClass("f"); // Should compile
MyClass obj2 = MyClass("fo"); // Should compile
MyClass obj3 = MyClass("foo"); // Should fail with same error as for t_mystruct s3 = {"foo"}
Is it possible to make the compiler check the stringlength of the constructor argument? Any help sooo much appreciated!