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?
Related
The argv array is a list pointers, each pointing to the respective argument, with the first element the number of command line arguments, correct?
My question is how do I pass a string argument to a function? In this small example I'm just trying to print the text I write as argument in the command line.
#include <stdio.h>
#include <stdlib.h>
void ptest(char *);
int main(int argc, char const *argv[])
{
ptest(argv[1]);
return 0;
}
void ptest(char *ptr)
{
printf("%s\n", ptr);
}
This code does not compile. It gives the following compilation errors:
teste2.c:8:8: warning: passing 'const char *' to parameter of type 'char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
ptest(argv[1]);
^~~~~~~
teste2.c:4:18: note: passing argument to parameter here
void ptest(char *);
^
That's not you issue. Your issue is that you've added the const keyword, meaning that you have a pointer to an array of values, where the array of values can't be changed.
You can check out this for a better explanation.
In your code:
int main(int argc, char const *argv[])
remove the const and your code will compile:
int main(int argc, char *argv[])
#include <stdio.h>
#include <string.h>
struct students{
char name[50];
int age;
int height;
};
int main(int argc, char **argv)
{
struct students manoj;
strcpy(manoj.name, "manojkumar");
manoj.age = 15;
displaymanoj(&manoj); //print testing \n , name , age
return 0;
}
void displaymanoj(struct students *ptr) {
printf("Testing...............DEBUG\n");
printf("%s\t%d\n", ptr->name,ptr->age);
printf("END OF TEST: SUCESS -manoj-");
}
I am learning C and it's working where is use pointer to point to structure variable. I am getting the correct output when I run the program. Just that my Geany IDE giving out some message which I would like to know why.
My Compiler Message as below :
You must declare the functions before calling them.
So your program should look something like
// Includes
// Structure
// Function prototype declaration
// This was what you were missing before
void displaymanoj(struct students *ptr);
int main(int argc, char **argv)
{
...
}
void displaymanoj(struct students *ptr) {
...
}
Since you have the definition of displaymanoj() isn't seen when you call it from main(), compiler implicitly declares one with return type int
which conflicts with the actual one. Note that the implicit declaration has been removed since the C99 standard and is no longer valid.
To fix it:
1) Either move the function displaymanoj() above main()'s definition or
2) Do a forward declaration of displaymanoj().
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scanner.h"
int WhatFell(char *typeoffood)
{
if (strcmp(typeoffood,"meat") == 0);
return 1;
}
void getData(char *typeoffood)
{
printf("What fell on the floor? ");
typeoffood = readToken(stdin);
return;
}
int main(int argc, char **argv)
{
char *typeoffood;
int x;
getData(typeoffood);
x = WhatFell(typeoffood);
printf("%s\n",typeoffood);
printf("%d\n",x);
return 0;
}
eat.c: In function ‘main’:
eat.c:14:12: warning: ‘typeoffood’ is used uninitialized in this function [-Wuninitialized]
getData(typeoffood);
^
A few notes:
'readToken' is found in the "scanner.h" inclusion and simply is a safe version of scanf() for strings.
Also please just focus on the error, this is just a snippet of code I wrote seeings if I would be able to use a function getData for string input in my program.
I'm trying to use a function to ask for user string input (which I can do fine with integers/reals) and then use the string to run another function, but I keep getting all these weird warnings though and if I run it i get a segmentation fault.
char *typeoffood;
int x;
getData(typeoffood);
typeoffood is not initialized but passed to getData() so would receive uninitialised data. NB: The numbers 12:14 in the message tell you the line numbers relevant to the error.
You should pass typeoffood as a pointer to a pointer:
getData(&typeoffood);
and give getData() the prototype:
void getData(char **);
we declare main() as
int main(int argc, char *argv[])
and pass some argument by command line and use it
as argv[1], argv[2] in main() function definition but what if i want to use that in some other function's definition ?
one things i can do it always pass that pointer char** argv from main() to that function by argument. But is there any other way to do so?
Just pass the pointer?
int main(int argc, char** argv) {
do_something_with_params(argv);
}
void do_something_with_params(char** argv) {
// do something
}
Or if you mean passing single arguments:
int main(int argc, char** argv) {
do_something_with_argv1(argv[1]);
do_something_with_argv2(argv[2]);
}
void do_something_with_argv1(char* arg) {
// do something
}
void do_something_with_argv2(char* arg) {
// do something
}
In order to make data available to other functions, you need to pass it as a parameter, or make it available through a global (not recommended) or a static variable.
static char** args cmd_args;
static int cmd_arg_count;
int main(int argc, char** argv) {
cmd_arg_count = argc;
cmd_args = argv;
do_work();
return 0;
}
void do_work() {
if (cmd_args > 1) {
printf("'%s'\n", cmd_args[1]);
}
}
The best approach is to make a function that parses command line parameters, and stores the results in a struct that you define specifically for the purpose of representing command line arguments. You could then pass that structure around, or make it available statically or globally (again, using globals is almost universally a bad idea).
one things i can do it always pass this value from main() to that function by argument. But is there any other way to do so?
No. Passing the argc and argv to other functions is perfectly valid.
int main(int argc, char *argv[])
{
typedef struct _cmdline_arg_struct {
// all your command line arguments go here
}cmdline_arg_struct;
/* command line arguments - parsed */
cmdline_arg_struct *pc = (cmdline_arg_struct *) malloc(sizeof(cmdline_arg_struct));
if (parse_cmdline_args(&pc, argc, argv) == PARSE_FAILURE) {
usage();
return 0;
}
/* Now go through the structure and do what ever you wanted to do.. *?
}
You could have code in main() to store the values in non-local variables, and then refer to those variables from other functions.
If you really need to, store them in globals.
but what if i want to use that in some other function's definition?
You mean have another set of parameters for main? That's not possible, because main is defined in the C standard as either
int main();
or
int main(int, char*[]);
And it makes sense: The first parameter tells you the number of parameters given on the command line, the second contains the array of pointers to those parameters.
It's your responsibility to make sense of those parameters and pass them to other functions. Use string conversion functions if you need to make them numbers.
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);
}