Pointer to a constant pointer...
Hail all you C gurus!
I've got a slight pointer mess I really need some help on, namely how to declare a variable as a non-constant pointer to an array of constant pointers to constant characters.
Basically a variable declaration for targv as follows:
(const char**)(non-const)*targv;
See the problem? I have the following variable:
const char **argv;
and need to assign the address of it to another pointer:
targv = &argv
where the actual pointer targv is NOT constant. ie it must be possible to increment targv (targv++), after which it should point to the address of the second string in argv.
The reason I need targv at all is that I want to pass its address (&targv) to a function, which can increment targv, although it can't make changes to the strings themselves.
That allows me to call two such functions in a row, with the second function continuing where the first left off.
Confused yet? If not, I'm sure you can help me out, 'cause I'm confused myself ;)
Oh, btw, yes, I'm sure there are other ways to tackle the problem in general, but I'm curious if this is doable in plain old c the way I described it. If you want me to further explain some part of the above, please ask!
Thanks in advance for any help!
Zen
Just as a general rule, if you tepdef things, it becomes much easier and clearer to build complex pointer declerations.
I'll post again in a min with some actual code once I read it over a few more times unless someone beates me to it...
Doh, I reread it and it made pleanty of sence... try this:
Dwiel
I'll post again in a min with some actual code once I read it over a few more times unless someone beates me to it...
Doh, I reread it and it made pleanty of sence... try this:
typedef const char **ccharpp;// now do something like:ccharpp argv;ccharpp* targv;targv = &argv
Dwiel
char* [pointer to a char]
const char* [pointer to a const char]
char* const [const pointer to a char]
const char* const [const pointer to a const char]
const char* const* const* ....
Now, I'm not exactly sure what you wan't, but this code is legal.
const char** p;
const char*** ptr;
ptr = &p
ptr++;
ptr--;
*ptr = p;
Hope that helps.
const char* [pointer to a const char]
char* const [const pointer to a char]
const char* const [const pointer to a const char]
const char* const* const* ....
Now, I'm not exactly sure what you wan't, but this code is legal.
const char** p;
const char*** ptr;
ptr = &p
ptr++;
ptr--;
*ptr = p;
Hope that helps.
"non-constant pointer to an array of constant pointers to constant characters"
Let's break that down.
const char x; // A constant character; easy enough
const char *x; // A non-constant pointer to a constant character
const char * const x; // A constant pointer to a constant character
const char * const * x; // A non-constant pointer to an array of (or possibly just 1) constant pointers to a constant character
If you said exactly what you wanted, then that last line is what you want.
Let's break that down.
const char x; // A constant character; easy enough
const char *x; // A non-constant pointer to a constant character
const char * const x; // A constant pointer to a constant character
const char * const * x; // A non-constant pointer to an array of (or possibly just 1) constant pointers to a constant character
If you said exactly what you wanted, then that last line is what you want.
Quote:Original post by Aprosenf
"non-constant pointer to an array of constant pointers to constant characters"
Let's break that down.
const char x; // A constant character; easy enough
const char *x; // A non-constant pointer to a constant character
const char * const x; // A constant pointer to a constant character
const char * const * x; // A non-constant pointer to an array of (or possibly just 1) constant pointers to a constant character
If you said exactly what you wanted, then that last line is what you want.
Hehe, constant character_s_ actually meant strings (ie char*), but close enough ;) I think I'll go with Tazzle's excellent suggestion of typedeffing it.
Just to save me another headache if I ever look at this code again in a few weeks.
For no good reason: the way I see it the next level of indirection (the one I want) would be:
const char const * * x; // A non-constant pointer to an array of constant pointers to an array of constant characters
If I made a mistake in there somewhere, please correct me.
Thanks!
Zen
Edit: Hope that fixes it...
Edit 2: Maybe now?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement