-
January 18th, 2006, 06:16 AM
#1
Is this valid initialize of char []
Hi,
I see the initializition of a char array like this:
Code:
char ident[] = "@(#) You are running version "SOFTVERSION ;
SOFTVERSION is a char *;
Is this a valid char [] declaration & init for ASCII C standard?
Thanks a lot in advance!
sandodo
-
January 18th, 2006, 06:24 AM
#2
Re: Is this valid initialize of char []
Originally Posted by sandodo
Code:
char ident[] = "@(#) You are running version "SOFTVERSION ;
SOFTVERSION is a char *;
Is this a valid char [] declaration & init for ASCII C standard?
No...
-
January 18th, 2006, 06:28 AM
#3
Re: Is this valid initialize of char []
sandodo: were you able to compile this succesfully?
-
January 18th, 2006, 06:31 AM
#4
Re: Is this valid initialize of char []
Is SOFTVERSION a char * or a string literal?
You are allowed to paste together two string literals.
-
January 18th, 2006, 07:16 AM
#5
Re: Is this valid initialize of char []
Sorry, the SOFTVERSION is actually a define
Code:
#define SOFTVERSION "5.46"
by the way, is it possible or isthere a way to do init a char* [] like this?
Code:
#define str "a string"
char* dent[] = {"@(#)"str}; //or char* dent[] = {str};
I am not quite sure what is the meaning of @#, can you please explain it?
Thanks!
sandodo
-
January 18th, 2006, 07:26 AM
#6
Re: Is this valid initialize of char []
If you do this
Code:
#include <iostream.h>
#define str " a string"
int main(int argc, char* argv[])
{
char* dent[] = {"test" str};
cout << *dent << endl;
return 0;
}
Then your output will be :
@# is just an example of a string, you can substitute it with anything.
-
January 18th, 2006, 08:08 AM
#7
Re: Is this valid initialize of char []
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
January 18th, 2006, 08:28 AM
#8
Re: Is this valid initialize of char []
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:
Code:
char * const_str = "a test";
char* dent[] = {const_str};
it doesnot work, is there a way to walk around without doing as following?:
Code:
char * const_str = "a test";
char* dent[] = {0};
dent[0] = const_str;
Thanks!
sandodo
-
January 18th, 2006, 10:06 AM
#9
Re: Is this valid initialize of char []
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;
-
January 18th, 2006, 11:55 AM
#10
Re: Is this valid initialize of char []
no,
Code:
char* dent[] = {0};
is declared as an array of char * and dent[0] is init to be NULL
I am considering assign an array of char* to some char* variables passed into a function as arguments.
-
January 18th, 2006, 12:45 PM
#11
Re: Is this valid initialize of char []
Originally Posted by sandodo
no,
Code:
char* dent[] = {0};
is declared as an array of char * and dent[0] is init to be NULL
I am considering assign an array of char* to some char* variables passed into a function as arguments.
Oops, my bad. You're right.
So what's with the curly braces? It's legal of course, but it sure looks odd to me. What's the purpose?
-
January 18th, 2006, 01:05 PM
#12
Re: Is this valid initialize of char []
Originally Posted by sandodo
Code:
char * const_str = "a test";
char* dent[] = {const_str};
it doesnot work, is there a way to walk around?
Yes,
Code:
char const_str[] = "a test";
char* dent[] = {const_str};
-
January 18th, 2006, 11:19 PM
#13
Re: Is this valid initialize of char []
char *const_str = "a test";
Never ever do that.
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";
C++ program ran... C++ program crashed... C++ programmer quit !!
Regards
Shaq
-
January 19th, 2006, 03:46 AM
#14
Re: Is this valid initialize of char []
to microcode: it is for initialization of the array. maybe you can see more clearly as following example:
Code:
char* dent[] = {NULL, NULL};
for approach:
Code:
char const_str[] = "a test";
char* dent[] = {const_str};
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* dent[2] = {0};
//many lines of other declarations between
dent[0] = const_str1;
dent[1] = const_str2;
Thanks!
sandodo
Last edited by sandodo; January 19th, 2006 at 04:01 AM.
-
January 19th, 2006, 04:00 AM
#15
Re: Is this valid initialize of char []
Originally Posted by sandodo
dent[0] = const_str1;
dent[1] = const_str2;
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|