Since a char ** is a pointer to a pointer, you can't do:
char foo[20][50];
char **ptr=&foo[0][0];
You're trying to use ptr to point to the first element of foo. The problem is that the first element of foo is a char and ptr needs to point to a pointer.
And you also can't do this for the same reason:
void bar (char **ptr) { /* ... */ }
/* ... */
char foo[20][50];
bar(&foo[0][0]);
(or any variation)
In other words, there is no pointer in foo to point to.
I'm assuming you want to use a pointer to iterate through the elements of your two dimensional array, perhaps via a function call?
One way to work it out is just to use an ordinary pointer to reference your multidimensional array in the function parameter and also include parameters for the dimension sizes:
/* Get memory for the pointers */
args=malloc(X_LENGTH * sizeof *args);
if (NULL!=args) {
/* Get memory for and copy over the strings */
for (i=0;i<X_LENGTH;++i) {
if (foo[0]) {
args=malloc(strlen(foo)+1);
if (NULL!=args) {
strcpy(args,foo);
}
} else {
args=NULL;
}
}
}
/* You can call execvp like this */
execvp(prog,args);
/* Sometime later, don't forget to free */
if (NULL!=args) {
for (i=0;args!=NULL;++i) {
free(args);
}
free(args);
}
return 0;
}
If you really /must/ have the program name and the args in the same array, just adjust the indexes and malloc'ed amounts appropriately.
Easy way to copy:
int main ()
{
char a[ARGC][SIZE2] = {some initialization};
char (*b)[SIZE2] = NULL;
int i;
b= (void*) malloc (ARGC*sizeof(char[SIZE2]));
for (i=0; i<ARGC; i++)
memcpy (b,a,sizeof(char[SIZE2]));
return 0;
}
!!!!!!
BUT THIS IS NOT IMOPRTANT. I SEE YOU DOTH DON'T UNDERSTAND ONE IMPORTANT THING:
!!!!!!
int b[10];
b == &b;
Array name in an expression is treated by the compiler as a pointer to the first element of the array (ANSI C Standard, paragraph 6.2.2.1). Array address is already taken with the & operator. So Compiler ignores "&b" and reads it as "b". &b == b.
so:
my_func(&b);
is the same as
my_func(b);
This can lead to an error when inside of function you are trying to take extra "*" from an argument.
STOP adding extra "&" to stack arrays!!!
Hope this will help and sorry for extra "!!!"
It's doing that because tek-tips interprets the i enclosed in brackets as a symbol for italics.
> b= (void*) malloc (ARGC*sizeof(char[SIZE2]));
There is no reason to cast the return value of malloc(). Furthermore, malloc() already returns void * so this cast is superfluous anyway. And why even use the sizeof operator when you can just:
b=malloc(ARGC * SIZE2);
sizeof char is /always/ 1, by definition.
As to your last comment regarding the usage of & in array expressions:
char b[]="foo";
myfunc(b);
myfunc(&b);
are /not/ equivalent.
These are equivalent:
myfunc(b);
myfunc(&b[0]);
Assuming that you meant this, I'm quite confused as to why you so strongly discourage using the latter method. In fact, one might argue that using the & operator in such an expression might clarify things in some contexts:
int main(void)
{
char s[]="foo";
foo(&a[0]);
return 0;
}
I think it would be a reasonable argument to say that this clarifies things in the sense that the caller of the function is making it explicit that the address of the first element of the array is being passed to foo(), suggesting that the value of a will be modified in foo(). Yes, foo() states this explicitly, but suppose foo() were somewhere else hidden in a library somewhere?
I also don't understand your point as to dereferencing an extra * from an argument inside of a function because the caller chose to use &a[0] instead of a. The function argument is the same regardless of the syntax the caller uses to call the function.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.