Hello I tried to get a external parameters to my main. But somehow the compiler gcc shows me weird problems I have not encountered them and did not find a solution. I would be happy if you help me understand the problems and fix them.
code-
#include <stdio.h>
#include <stdlib.h>
main(int argc, char** argv[])
{
int i;
char temp[99];
for (i=0; i < argc/2 ; i++)
{
temp = argv[i];
argv[i] = argv[argc-i-1];
argv[argc-i-1] = temp;
}
for(i = 0; i < argc ; i++)
{
printf("%s",temp[i]);
}
return(0);
}
errores
error: incompatible types in assignment
warning: assignment from incompatible pointer type
Bad prototype for main: Use int main(), int main(int argc, char** argv) or something compatible.
Related
Summary
I am trying to pass the text that user has put in the terminal and pass it to the function named printString(). I don't fully understand C but I think I has to do with the pointer not being in the heap. Any help would be appreacitaed!
#include <stdio.h>
void printString();
int main (int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
return 0;
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s," , argv[commandAmount]);
}
./shortExample example
Segmentation fault (core dumped)
The prototype void printString(); does not match the actual implementation.
It should have been:
void printString(int commandAmount, char* argv[]);
You could also skip the prototype and just implement the function before main. Your loop while (commandAmount < argc) seems to not have any way to finish since you never increase commandAmount. This can cause undefined behavior and with such, your program may crash or do just about anything.
I suggest making a for-loop to fix that.
Example:
#include <stdio.h>
void printString(int commandAmount, char* argv[]) {
printf("the word is %s,", argv[commandAmount]);
}
int main(int argc, char* argv[]) {
for(int commandAmount = 1; commandAmount < argc; ++commandAmount) {
printString(commandAmount, argv);
}
}
or in the way you structured it:
#include <stdio.h>
void printString(int commandAmount, char* argv[]); // corrected
int main(int argc, char* argv[]) {
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, argv);
++commandAmount; // needed
}
}
void printString(int commandAmount, char* argv[]) {
printf("the word is %s,", argv[commandAmount]);
}
For starters this function declaration
void printString();
does not provide a function prototype. So the compiler determines the type of the parameters of the function from the function call
printString(commandAmount, &argv);
However the expression &argv used in this call
printString(commandAmount, &argv);
has the type char *** due to the declaration of the identifier argv
int main (int argc, char* argv[]) {
^^^^^^^^^^^^
But the corresponding parameter in the definition of the function printString has the type char ** due to adjusting by the compiler parameters having array types to pointers to array element type.
That is this function declaration
void printString(int commandAmount, char* argv[]) {
is adjusted by the compiler to
void printString(int commandAmount, char** argv) {
^^^^^^^^^^^
Thus there are incompatible types of the argument expression and of the parameter. As a result this call
printf("the word is %s," , argv[commandAmount]);
invokes undefined behavior.
Moreover this loop in main
int commandAmount = 1;
while (commandAmount < argc) {
printString(commandAmount, &argv);
}
in general is an infinite loop because the variable commandAmount is not changed within the loop.
Firstly you should provide the function prototype before main to make your program more safer
void printString(int commandAmount, char** argv);
and call the function like
printString(commandAmount, argv);
^^^^
Of course you need also to change the loop in main.
Pay attention to that as the value of the parameter commandAmount is not outputted within the function then in fact it is redundant. You could pass to the function the pointer to the string itself. For example
#include <stdio.h>
void printString( const char *s );
int main( int argc, char* argv[] )
{
for ( int commandAmount = 1; commandAmount < argc; commandAmount++ )
{
printString( argv[commandAmount] );
}
putchar( '\n' );
return 0;
}
void printString( const char *s )
{
printf( "the word is %s, " , s );
}
here is my code
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char* argv[])
{
int a;
for(int i=1;i<=argc;i++){
a+=atoi(argv[i]);
}
printf ("%d",a);
}
I keep getting segmentation faults but i am trying to add up all elements of the command line so for example ./a.out 5 6 7 would give 18 as the output, cheers.
The problem (with the crash) is the loop itself:
for(int i=1;i<=argc;i++)
The argc argument is the number of arguments passed to the program, including the "program name" at argv[0]. So valid indexes for the actual arguments are argv[1] to argv[argc - 1].
Furthermore the argv array is terminated by a null pointer, which will be at argv[argc].
Since you include argv[argc] in your loop you pass a null pointer to atoi which leads to undefined behavior and likely crashes.
The simple solution is to use less-than < instead of less-than-or-equal as the loop condition:
for(int i=1;i<argc;i++)
You never initialized a to 0. Also, use strtol() function.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int a = 0;
for (int i = 1; i < argc; i++) {
a += strtol(argv[i], NULL, 10);
}
printf("%d\n", a);
return EXIT_SUCCESS;
}
So, I have been working on this simple block of code. I would like it to print, when I type in "./a.out -n"
However, that is not working. I have been on stackoverflow trying to work on this, but no such luck. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
void parse_cmdline(int argc, char *argv);
int main (int argc, char *argv[]) {
parse_cmdline(argc, argv);
}
void parse_cmdline(int argc, char *argv)
{
int x,i,m,n = 0;
if (*(++argv) == 'n'){
x = 1;
printf("Output array: "); /* not being displayed*/
}
}
Just write
if (**++argv == 'n'){
And the function should be declared like
void parse_cmdline(int argc, char **argv);
Otherwise you should specify what parameter you are going tp pass to the function. For example
parse_cmdline(argc, argv[1]);
You can check what parameters are passed to the program the following way
int main (int argc, char *argv[]) {
for ( int i = 0; i < argc; i++ ) puts( argv[i] );
}
Try:
#include <stdio.h>
#include <stdlib.h>
void parse_cmdline(int argc, char *argv[]);
int main (int argc, char *argv[]) {
parse_cmdline(argc, argv);
}
void parse_cmdline(int argc, char *argv[])
{
int x,i,m,n = 0;
if (*(argv[1]) == '-' && *(++argv[1]) == 'n'){
x = 1;
printf("Output array: "); /* not being displayed*/
}
}
And run it with ./a.out -n.
So, I have examined 0th and 1th character of the argv value on the position one ("./a.out" is located on position 0, and "-n" on position 1).
Is that what you wanted?
Also, you cannot ignore warnings:
1.c: In function ‘main’:
1.c:5:23: warning: passing argument 2 of ‘parse_cmdline’ from incompatible pointer type [-Wincompatible-pointer-types]
parse_cmdline(argc, argv);
^
1.c:3:6: note: expected ‘char *’ but argument is of type ‘char **’
void parse_cmdline(int argc, char *argv);
If you write
parse_cmdline(argc, argv);
then parse_cmdline should be
void parse_cmdline(int argc, char *argv[]);
I searched documentation for indent, but I gave up eventually, I want to indent code like this:
int main(int argc, char **argv){
some code;
}
I know indent -kr gives you braces like this, but -kr style also includes
int
main(int argc, char **argv){
some code;
}
and this int in line before main gives me creeps.
Can anyone please tell me option for this?
The particular options that you are interested in are
-npsl
--dont-break-procedure-type
Put the type of a procedure on the same line as its name.
-brf
--braces-on-func-def-line
Put braces on function definition line.
As suggested, the GNU indent manual describes the various options.
Here is a quick script to illustrate the effect of those options on the basic predefined styles:
#!/bin/sh
for opt in gnu linux orig kr
do
echo "** $opt"
indent -st -$opt -npsl -brf hello.c
done
and the input file:
#include <stdio.h>
int main(int argc, char **argv) { int n; for (n = 0; n < argc; ++n) printf("arg%d=%s\n", n, argv[n]); return 0; }
and corresponding output:
** gnu
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
** linux
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
** orig
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
** kr
#include <stdio.h>
int main(int argc, char **argv) {
int n;
for (n = 0; n < argc; ++n)
printf("arg%d=%s\n", n, argv[n]);
return 0;
}
I do not see an option to suppress the space before the { character.
You are likely looking for the -npsl option. The -psl (--procnames-start-lines) option causes the type of a procedure being defined to be placed on the line before the name of the procedure. Thus if you specify that option, all function declarations will be changed from:
int main(int argc, char **argv){
some code;
}
to
int
main(int argc, char **argv){
some code;
}
You can check whether you are including -psl in the common type -kr and remove it, or if not included, you can specify -npsl (--dont-break-procedure-type) and the type will not be placed on a separate line.
There are trade-off with all options. I like braces on the same line as int main() {, but for function definitions I like braces on the next line. e.g.:
type function
{
...
}
So if you have mutually exclusive preferences such as that, you simply choose the one you want a majority of the code to incorporate and tweak the rest. You might give the following invocation a try:
indent -i 4 -lp -ts 8 -lp -lps -br -brs -blf -ce -cdw -pcs -bs -nbc -npsl -saf -sai -saw -nut
It is somewhat of a balanced indent scheme.
I'm trying to write a program that takes a string as a command line argument and then runs said argument through a function (str_to_int) that takes a string as an input. However, when I try to compile the program, I get a warning saying
initializing 'char *' with an expression of type 'int' [-Wint
conversion]
char* str = atoi(argv[1]);
^ ~~~~~~~~~~~~~
And when I run the program I get a segmentation fault
I've tested the str_to_int a lot so I'm pretty sure that the issue lies with the command line program. Here's the code for it.
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = atoi(argv[1]);
printf("%d\n", str_to_int(str));
return 0;
}
Can anyone tell me what I'm doing wrong? Thanks.
This is all you need, though it will crash if you leave out the command-line argument.
{
printf("%d\n", str_to_int(argv[1]));
return 0;
}
This is more robust:
int main(int argc, char *argv[])
{
if (argc == 1)
printf("missing parameter.");
else
printf("%d\n", str_to_int(argv[1]));
return 0;
}
#include "hw3.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
char* str = argv[1];
printf("%d\n", str_to_int(str));
return 0;
}
just remove atoi function invocation and it should work