Reputation: 91
I'm having a problem casting, so is there a way to cast a type of:
char *result;
to a type of
char *argv[100];
?
If so, how would I do this or is there a safe way to do this?
Upvotes: 2
Views: 247
Reputation: 3807
First, a short explanation:
char *result;
The variable result
is a pointer and when set it will point to a single character. However, as you know, a single character can be the start of a string that ends with the null (\0) character.
In C, a good programmer can use the pointer result
to index through a string.
However, the string's length is NOT known until the pointer reaches a null character.
It is possible to define a fixed length string of characters, in this case code:
char s[100];
Now, the fun begins. s
per Kernighan and Ritchie (K&R) is a pointer to a string of characters terminated with a 0.
So, you can code:
s[0] = 'a';
*s = 'a';
s[1] = 'b';
*(s+1) = 'b';
These are equivalent statements.
As mentioned in other posts, let's add explicit parens to your argv statement:
char *(argv[100]);
Thus, this is an array of 100 pointers to characters (each of which might or might not be the start of a string of characters).
Upvotes: 0
Reputation: 601
char *result
is a pointer to a char
char *argv[100]
is an array of char *
, so really it's a char **
(a pointer to pointers)
Keep this in mind:
int* arr[8];
// An array of int pointers.
int (*arr)[8];
// A pointer to an array of integers
This being the case, this is probably not what you want to be doing. I suppose the next question is: What were you trying to do? Or why?
Upvotes: 2
Reputation: 123468
What does result
contain, and what do you expect argv
to contain after the conversion?
For example, if result
points to a list of strings separated by a delimiter, like "foo,bar,bletch,blurga,..
, and you want each string to be a separated element in argv
, like
argv[0] == "foo"
argv[1] == "bar"
argv[2] == "bletch"
argv[4] == "blurga"
then you could not accomplish this with a simple cast; you'd have to actually scan result
and assign individual pointers to argv
.
Upvotes: 0
Reputation: 366
char * result
is a string
and
char * argv[100]
is array of strings.
You cannot convert string into array of strings. However, you can create an array of strings where the first array value is result.
argv[0] = result;
Upvotes: 4