Hi,
I see the initializition of a char array like this:
SOFTVERSION is a char *;Code:char ident[] = "@(#) You are running version "SOFTVERSION ;
Is this a valid char [] declaration & init for ASCII C standard?
Thanks a lot in advance!
sandodo
Printable View
Hi,
I see the initializition of a char array like this:
SOFTVERSION is a char *;Code:char ident[] = "@(#) You are running version "SOFTVERSION ;
Is this a valid char [] declaration & init for ASCII C standard?
Thanks a lot in advance!
sandodo
No...Quote:
Originally Posted by sandodo
sandodo: were you able to compile this succesfully?
Is SOFTVERSION a char * or a string literal?
You are allowed to paste together two string literals.
Sorry, the SOFTVERSION is actually a define
by the way, is it possible or isthere a way to do init a char* [] like this?Code:#define SOFTVERSION "5.46"
I am not quite sure what is the meaning of @#, can you please explain it?Code:#define str "a string"
char* dent[] = {"@(#)"str}; //or char* dent[] = {str};
Thanks!
sandodo
If you do this
Then your output will be :Code:#include <iostream.h>
#define str " a string"
int main(int argc, char* argv[])
{
char* dent[] = {"test" str};
cout << *dent << endl;
return 0;
}
@# is just an example of a string, you can substitute it with anything.Code:test a string
The preprocessor does its job before the actual compilation kicks in and hence that way of initialization is okay but why would you want to do that? Why don't you have file-scoped constant string literals. Regards.
Thanks for all your replies.
I think it is supposed to let programmer only modify the define of SOFTVERSION so that all other part of the codes know that now version changes to a new one.
And I tried this for init of a array:
it doesnot work, is there a way to walk around without doing as following?:Code:char * const_str = "a test";
char* dent[] = {const_str};
Thanks!Code:char * const_str = "a test";
char* dent[] = {0};
dent[0] = const_str;
sandodo
I'm not sure what you're trying to do with those curly braces, but they don't belong there. And dent[0] refers to a single character but const_str refers to an array of characters, so that assignment is invalid. I think what you want is simply:
Code:const char * const_str = "a test";
// ...
const char * dent = const_str;
no,is declared as an array of char * and dent[0] is init to be NULLCode:char* dent[] = {0};
I am considering assign an array of char* to some char* variables passed into a function as arguments.
Oops, my bad. You're right.Quote:
Originally Posted by sandodo
So what's with the curly braces? It's legal of course, but it sure looks odd to me. What's the purpose?
Yes,Quote:
Originally Posted by sandodo
Code:char const_str[] = "a test";
char* dent[] = {const_str};
Never ever do that.Quote:
char *const_str = "a test";
When using a pointer to point to a const string like above, make sure to use const.
Always declare like this...
Code:const char *const_str = "a test";
to microcode: it is for initialization of the array. maybe you can see more clearly as following example:for approach:Code:char* dent[] = {NULL, NULL};
The problem is that const_str1, const_str2 are passed into the function as char* type arguments. currently in that function what I do is, though it is not good approach since I will have to init the dent far from its declaration,Code:char const_str[] = "a test";
char* dent[] = {const_str};
Code:char* dent[2] = {0};
//many lines of other declarations between
dent[0] = const_str1;
dent[1] = const_str2;
Thanks!
sandodo
I don't think here you will face any issues if you are using const char arrays (declared as constants) to initialize dent elements. But take care that you cannot change string elements of dent after that.. for that capability - you would instead of above, use strcpy. Regards.Quote:
Originally Posted by sandodo
Thanks, but I tried to init den as following, and I changed the arguments type of const_str1 & const_str2 to const char*
const char* dent[] = {const_str1, const_str2};
the compiler reports (sorry not to give the compiler type, it is cc in HP Unix server):
cc: error 1521: Incorrect initialization.
That's not a problem. A function with formal parameter of type pointer can always take a compatibly typed array as an actual parameter.Quote:
Originally Posted by sandodo