I'm trying to create an extremely simple program that consists of two .c files - main.c and foo.c.
我正在尝试创建一个非常简单的程序,它包含两个.c文件 - main.c和foo.c.
The objective is to link both these files, and pass a parameter into the main.c method, which then calls method foo() in the foo.c file.
目标是链接这两个文件,并将参数传递给main.c方法,然后在foo.c文件中调用方法foo()。
The code for main.c is:
main.c的代码是:
#include <stdio.h>
#include "foo.h"
int main(int argc, char *argv[]) {
foo(argv[1]);
return 0;
}
and foo.c is:
和foo.c是:
#include <stdio.h>
#include "foo.h"
void foo(char *input[]) {
printf("Welcome, %s\n", input);
}
I also have a header file, foo.h which has just the line: void foo(char *input[]);
我还有一个头文件,foo.h,它只有一行:void foo(char * input []);
When I try to build and run the files using
当我尝试使用构建和运行文件时
gcc -o main main.c foo.c foo.h
I get the error
我收到了错误
main.c: In function ‘main’:
main.c:6:2: warning: passing argument 1 of ‘foo’ from incompatible pointer type [enabled by default]
foo(argv[1]);
^
In file included from main.c:2:0:
foo.h:1:6: note: expected ‘char **’ but argument is of type ‘char *’
void foo(char *input[]);
Where have I gone wrong in my code? I've been trying to play around with it for a while. Note that when I run ./main "Test", it prints Welcome, Test i.e. the correct expected output, but the warning errors bother me. Also, how can I make a simple makefile to compile these 3 files, rather than typing gcc everytime? I tried to do the following:
我的代码在哪里出错了?我一直试图玩它一段时间。请注意,当我运行./main“Test”时,它会打印Welcome,Test,即正确的预期输出,但警告错误会让我烦恼。另外,如何编写一个简单的makefile来编译这3个文件,而不是每次都输入gcc?我试着做以下事情:
CFLAGS=-Wall -g
all: main foo
clean:
rm -f main foo
but typing "make" doesn't compile anything.
但输入“make”不会编译任何东西。
Thanks.
谢谢。
2 个解决方案
#1
5
I don't know where did you take this
我不知道你把它带到了哪里
void foo(char *input[]) {
printf("Welcome, %s\n", input);
}
from, but it's certainly wrong.
从,但它肯定是错的。
It should be
它应该是
void foo(char *input) {
printf("Welcome, %s\n", input);
}
the type of argv[1]
is char *
and using char input[]
although valid, is not necessary because it's converted to a pointer anyway, even char input[10]
will have type char *
, this is specially bad if you try to use the sizeof
operator.
argv [1]的类型是char *并且使用char input []虽然有效,但是没有必要,因为它无论如何都转换为指针,即使char输入[10]也会有char *类型,如果你试图这个特别糟糕使用sizeof运算符。
In my opinion, and it's just my opinion, you should never make your code Java-ish like void foo(char input[])
, some people have an argument that states it's good to do it to know that you are passing an array to the funcion, but it doesn't matter because in the end it's just a pointer to the first element of the array anyway.
在我看来,这只是我的意见,你永远不应该使你的代码Java-ish像void foo(char input []),有些人有一个参数,声明它是好的,知道你传递一个数组到函数,但它没关系,因为最后它只是指向数组的第一个元素的指针。
#2
3
In here void foo(char *input[])
input is array of type char pointer which should be just void foo(char input[])
. The rule should be obeyed is the actual argument passed and the formal argument in the function called which is received should of same type.
在这里,void foo(char * input [])输入是char指针类型的数组,它应该只是void foo(char input [])。应遵循的规则是传递的实际参数和被接收的函数中的形式参数应该是相同类型的。
#1
5
I don't know where did you take this
我不知道你把它带到了哪里
void foo(char *input[]) {
printf("Welcome, %s\n", input);
}
from, but it's certainly wrong.
从,但它肯定是错的。
It should be
它应该是
void foo(char *input) {
printf("Welcome, %s\n", input);
}
the type of argv[1]
is char *
and using char input[]
although valid, is not necessary because it's converted to a pointer anyway, even char input[10]
will have type char *
, this is specially bad if you try to use the sizeof
operator.
argv [1]的类型是char *并且使用char input []虽然有效,但是没有必要,因为它无论如何都转换为指针,即使char输入[10]也会有char *类型,如果你试图这个特别糟糕使用sizeof运算符。
In my opinion, and it's just my opinion, you should never make your code Java-ish like void foo(char input[])
, some people have an argument that states it's good to do it to know that you are passing an array to the funcion, but it doesn't matter because in the end it's just a pointer to the first element of the array anyway.
在我看来,这只是我的意见,你永远不应该使你的代码Java-ish像void foo(char input []),有些人有一个参数,声明它是好的,知道你传递一个数组到函数,但它没关系,因为最后它只是指向数组的第一个元素的指针。
#2
3
In here void foo(char *input[])
input is array of type char pointer which should be just void foo(char input[])
. The rule should be obeyed is the actual argument passed and the formal argument in the function called which is received should of same type.
在这里,void foo(char * input [])输入是char指针类型的数组,它应该只是void foo(char input [])。应遵循的规则是传递的实际参数和被接收的函数中的形式参数应该是相同类型的。