I happen to have several functions which access different arguments of the program through the argv[] array. Right now, those functions are nested inside the main() function because of a language extension the compiler provides to allow such structures.
I would like to get rid of the nested functions so that interoperability is possible without depending on a language extension.
First of all I thought of an array pointer which I would point to argv[] once the program starts, this variable would be outside of the main() function and declared before the functions so that it could be used by them.
So I declared such a pointer as follows:
char *(*name)[];
Which should be a pointer to an array of pointers to characters. However, when I try to point it to argv[] I get a warning on an assignment from an incompatible pointer type:
name = &argv;
What could be the problem? Do you think of another way to access the argv[] array from outside the main() function?
char ** name;
...
name = argv;
will do the trick :)
you see char *(*name) [] is a pointer to array of pointers to char. Whereas your function argument argv has type pointer to pointer to char, and therefore &argv has type pointer to pointer to pointer to char. Why? Because when you declare a function to take an array it is the same for the compiler as a function taking a pointer. That is,
void f(char* a[]);
void f(char** a);
void f(char* a[4]);
are absolutely identical equivalent declarations. Not that an array is a pointer, but as a function argument it is
HTH
This should work,
char **global_argv;
int f(){
printf("%s\n", global_argv[0]);
}
int main(int argc, char *argv[]){
global_argv = argv;
f();
}
#include <stdio.h>
int foo(int pArgc, char **pArgv);
int foo(int pArgc, char **pArgv) {
int argIdx;
/* do stuff with pArgv[] elements, e.g. */
for (argIdx = 0; argIdx < pArgc; argIdx++)
fprintf(stderr, "%s\n", pArgv[argIdx]);
return 0;
}
int main(int argc, char **argv) {
foo(argc, argv);
}
Related
I'm new to C and im currently learning about pointers.
I'm not sure why I am getting an error with the following sections of code in regards to pointers :
char ch;
char** pointer;
pointer = &ch;
and
int function1(void)
{
return 42.0;
}
void function2(void)
{
void (*pointer)(int);
pointer = &function1;
...
}
Any help will be appreciated :)
The very first problem is that you are using a double pointer in char** pointer ,as you are not storing the address of some other pointer so you should use char *pointer instead.
Then your function1 has return type as int but you are returning a float value ,although it won't give you any error but it can create some logical issues in your program,so better to properly write the return type in function definition and its prototype.
Then the next problem is in the function2,your function1 returns int but does not take any arguments but your function pointer return void and take int ,so you should better modify this to
int (*pointer)(void);
and then store the address of function1 in pointer ,it will work fine.
* is a single pointer and ** is a pointer to pointer.
So instead of
char** pointer;
It should be:
char* pointer;
In the second case, the function pointer prototype is not matching the prototype of the function it is pointing to.
So instead of
void (*pointer)(int);
it should be:
int (*pointer)(void);
you second section have some mistakes
you function1() return int and not take args
but your fucntion ptr return void and take int
so change it to:
int (*pointer)(void);
pointer = &function1;
This is my main.c:
#include <stdio.h>
void changeStuff(char *stuff){
stuff= "Hello everyone";
}
int main(int argc, char **argv) {
char *stuff;
changeStuff(stuff);
printf(stuff);
return 0;
}
When I build this, I get this warning:
warning: ‘stuff’ is used uninitialized in this function [-Wuninitialized]
When I run this program, nothing is printed.
Since it does not seem possible to define a char* with no value after it has been declared, how do I change the value of a char* passed to a function?
In C, function arguments are passed-by-value. In order to modify a pointer value, you need to pass a pointer to the pointer, like:
#include <stdio.h>
void changeStuff(char **stuff){
*stuff= "Hello everyone";
}
int main(int argc, char **argv) {
char *stuff;
changeStuff(&stuff);
printf("%s\n", stuff);
return 0;
}
Note that it's not a good idea to directly pass user-defined values to printf(). See: How can a Format-String vulnerability be exploited?
I am just trying to write something like that:
u64 get_env(char *argv[]);
char* g_argv[];
static char * Sample ()
{
return (char*)get_env(g_argv);
}
int main(int argc, char *argv[])
{
g_argv = argv;
Sample();
}
Getting error: 'g_argv' has an incomplete type
warning: array 'g_argv' assumed to have one element [enabled by default]
I've tried many different ways. How to write it right?
The compiler sees you declaring g_argv as an array of char pointers, but you don't specify how many.
int main(int argc, char *argv[])
Despite what it looks like, argv is not an array; arrays aren't first class objects in C and cannot be passed to functions, only their addresses can. So because argv is a parameter, it's actually a pointer to a pointer. For this reason I think it's better to tell the truth and use
int main(int argc, char** argv)
which is exactly equivalent to the above. This confusion in the language has led you to
char* g_argv[];
You're saying this is an array of pointers, without saying how big the array is, but that's not what you want; you want a pointer to the first of several pointers:
char** g_argv;
That fixes the problem you asked about, but I wonder about this declaration:
u64 get_env(char *argv[]);
Why declare it as returning u64 when the name and usage clearly indicate that it returns a char*? Actually, you should not be declaring it here at all ... it should be declared in a header file that specifies the API that includes get_env. Hopefully that header file declares it as returning a char*, and then you can remove the cast from
return (char*)get_env(g_argv);
My c skills are very rusty, so I apologize if this is a dumb question, but I could not even think were to look for the answer to this simple question.
This code compiles without any warnings:
#include <ruby.h>
int run_script( int argc, char *argv[] ) {
ruby_sysinit(&argc, &argv);
}
But when I compile this code, I get the following warning:
#include <ruby.h>
int run_script( char * parameters ) {
int argc=0;
char *argv[1];
ruby_sysinit(&argc, &argv);
}
run_script_3.c: In function 'run_script':
run_script_3.c:7: warning: passing argument 2 of 'ruby_sysinit' from incompatible pointer type
it seems like i am passing the same pointer type in both cases.
The problem is that an array in formal parameter declaration is equivalent to pointer. Declaration of
int run_script( int argc, char *argv[] ) { ... }
is equivalent to
int run_script( int argc, char **argv) { ... }
because you can not send an array as parameter. You can send only a pointer and even if you put an array as actual parameter it is always converted to a pointer. Then if you put &argv you are getting address of this parameter, so it is an address in system stack where argv's value is stored.
On the other hand array in code is still an array, which is different type. In your case &argv is of type char *** inside the first version of run_script while it is of type char *(*)[1] in the second version. It looks like the same type, but it is not. In run time, there is even bigger difference, because your two invocations are sending two completely different values to ruby_sysinit.
Your second example:
int run_script( char * parameters ) {
int argc=0;
char *argv[1];
ruby_sysinit(&argc, &argv);
}
will probably cause segfault because opearator & applied on an array gives the pointer to the first element of the array. So the call ruby_sysinit(&argc, &argv); is sending the same values as ruby_sysinit(&argc, argv);, i.e. a value which points to char*, not to pointer to char* as expected by ruby_sysinit.
The signature of ruby_sysinit is as follows (correct me if I picked the wrong ruby version, I don't have the header file on my system):
void ruby_sysinit(int *argc, char ***argv);
This means that the following compiles:
extern void ruby_sysinit(int *argc, char ***argv);
int run_script(char * params) {
int argc = 0;
char **argv;
ruby_sysinit(&argc, &argv);
}
Now, when you declare char *argv[1], you tell the compiler that this is an array with one element. This is not the same as char **argv.
EDIT: you might find this question on SO and this article (as linked in the other question) useful.
Need some help with type conversaion with string pointers in C. I have a function that gets the the *argv from main loop to pass command-line parameters to it. Since the parameters are fixed, I am trying to give my own *argv style parameter to it, but gcc everytime gives a warning:
passing argument 2 of ‘DirectFBInit’ from incompatible pointer type
Code:
int main (int argc, char *argv[])
{
...
char *argx[2] = {"self","--dfb:no-vt"};
char *argxPtr = argx;
DFBCHECK (DirectFBInit (&fakeArgc, &argxPtr));
...
}
I should mention that the function is manipulation argv (hence argx).
Here are the definition of DirectFBInit:
DFBResult DirectFBInit(
int *argc, /* pointer to main()'s argc */
char *(*argv[]) /* pointer to main()'s argv */
);
The prog is running but I'm concerned about it.
The web-site for DirectFB probably has useful information.
Your code should probably be:
int main(int argc, char **argv)
{
...
char *argvxData[] = { "self", "--dfb:no-vt", 0 };
char **argvx = argvxData;
int argcx = 2;
DFBCHECK(DirectFBInit(&argcx, &argvx));
...
}
Note the added null pointer to match the null pointer at argv[argc] in the main() program. I've not read the manual to ensure that's required, but consistency is good. When I checked the first edition of my answer, it did not compile without warnings — in fact, it got your warning; I've fixed that with the argvxData array and argvx double pointer. The [] in the prototype is mostly a red-herring, but avoids accusations of being a three-star programmer. It is equivalent to char ***argv.
Or you could pass the arguments to main:
DFBCHECK(DirectFBInit(&argc, &argv));
If we call char * "string" then that simplifies it a bit. The function expects a pointer to an array of strings. You are passing in a string, and an incorrectly-initialized string at that (argx has type char*[] but you are assigning it to a variable of type char*).
&argxPtr should actually be &argx (an expression of type char*(*[]) as expected by the function), and you don't need argxPtr at all.
Something like this?
int myargc = 2;
const char *myargv[] = { "self", "--dfb:no-vt" };
DirectFBInit(&myargc, &myargv);
A simple way to verify is to compile with -g and run gdb. Once in gdb, run the program, and type ptype and you will see the difference. I have replaced the func name with foo()
foo(int *argc, char *(*argv[]))
{
}
int
main (int argc, char *argv[])
{
char *argx[2] = {"self","--dfb:no-vt"};
char *argxPtr = argx;
foo (&argc, &argxPtr);
}
(gdb) ptype foo
type = int (int *, char ***)
(gdb) ptype main
type = int (int, char **)
Hence the warning. Hope this helps.