[SOLVED] C: How do you declare a function in advance?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
But if you look at the first prototype in post #1, you'll see there is no variable name there. I infer I could save the variable name in the second too, hence.
The variable name in a function prototype is meaningless, but you can put one there if you like. The name (or in your case, even the array size) does not need to match what appears in the actual function definition. Only the type is important.
bill@juan:~/TIMER/arrays$ cat u01.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int juan(char *);
int juan(char* a[10]){
/* The first dimension is actually ignored by the compiler. So
* * int juan(int a[][10]) is equally good. El segundo indice
es el
* * que varia mas rapidamente.
* */
printf("The values are %s and %s\n", a[0], a[1]);
return 0;
}
int main(){
char* a[10];
return 0;
}
bill@juan:~/TIMER/arrays$ gcc u01.c
u01.c:6:5: error: conflicting types for 'juan'
u01.c:5:5: note: previous declaration of 'juan' was here
bill@juan:~/TIMER/arrays$
If the function in question is above the point where it is used, you do not need to declare a prototype for it. Are you intentions to learn how to say place that function in another file so you can segment a lot of code and be able to call the function from outside file scope? Are your questions related more to the scope of the function as it applies to file versus project?
int f2(char *a1[100]); {
/* BODY OF THE FUNCTION */
}
How would you declare it?
This case is syntactically invalid, AFAIK. Either the declaration of function f2 is followed by a semicolon, which indicates a forward-declaration of that function, or it is immediately followed by the body of the function, in braces, with no semicolon or any other punctuation mark between them. When the "real" definition of the function appears, it must exactly match the previous forward-declaration ... although, fair warning(!), it's pretty much an honor-system. Unlike C++, the C linker does not validate parameter-passing.
Such forward-declarations customarily appear in .h files.
I think I understand. For every array b, b is a pointer pointing to the first of the elements of the array. So, in
char * a1[100];
a1 points to a1[0]. But according to the declaration, the elements of a1 are pointers themselves. So a1 points to a1[0] which in turn points to the first element of a1[0] (which is char). Hence a1 is pointer to pointer and I may write
char ** a1;
Actually it would be (char *) * a1, so I think the compiler associates from left to right in the expression above.
Yes, as function-parameter declaration they are the same thing.
Mind you, as variable declaration they are different: the first allocates 400 bytes (on 32-bit computer) for 100 pointers of type 'char *', the second allocates 4 bytes for 1 pointer of type 'char **'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.