LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2014, 10:18 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
C: How do you declare a function in advance?


I know how to do it in the following case:
Code:
int f1(char *);

int f1(char *a1){
/* BODY OF THE FUNCTION */
}
But what about this case?
Code:
int f2(char *a1[100]);{
/* BODY OF THE FUNCTION */
}
How would you declare it?

Last edited by stf92; 12-14-2014 at 11:25 AM.
 
Old 12-14-2014, 11:11 AM   #2
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
I don't understand your question. You've provided a prototype for your function f2(), so what else do you need, besides the implementation, i.e.

Code:
int f2(char *a1[100]) {
 // Code here
}
 
Old 12-14-2014, 11:24 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Sorry. That was meant to be the function itself. I'll will edit the post.
 
Old 12-14-2014, 11:44 AM   #4
ndc85430
Member
 
Registered: Apr 2014
Distribution: Slackware
Posts: 92

Rep: Reputation: Disabled
But your prototype

Code:
int f2(char *a1[100]);
looks fine to me. Why do you think there's a problem with it?
 
Old 12-14-2014, 11:52 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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.
 
Old 12-14-2014, 12:21 PM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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.
 
Old 12-14-2014, 12:37 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
But
Code:
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$
EDIT: It seems you were right. I put
Code:
int juan(char * []);
and the compiler accepted it.

Last edited by stf92; 12-14-2014 at 12:39 PM.
 
Old 12-14-2014, 12:45 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
There is a big difference between a character pointer (char *) and an array of character pointers (char *[]) or (char *a[]).
 
Old 12-14-2014, 12:57 PM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks.
 
Old 12-15-2014, 01:35 PM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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?
 
Old 12-15-2014, 04:15 PM   #11
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Quote:
Originally Posted by stf92 View Post
But what about this case?
Code:
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.
 
Old 12-16-2014, 03:41 AM   #12
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by stf92 View Post
EDIT: It seems you were right. I put
Code:
int juan(char * []);
and the compiler accepted it.
You could also have declared int juan(char **);
 
Old 12-16-2014, 12:16 PM   #13
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
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.

Last edited by stf92; 12-16-2014 at 12:21 PM.
 
Old 12-17-2014, 02:04 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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 **'
 
1 members found this post helpful.
Old 12-17-2014, 07:36 AM   #15
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thank you so much, NevemTeve.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
declare virtual function in derived class icecubeflower Programming 3 11-28-2009 09:04 PM
Declare Vs Define Ace Blackwell Programming 15 09-14-2008 08:23 PM
( C ) How do you declare a function pointer where a parameter is a function pointer? spursrule Programming 5 11-27-2007 07:56 PM
declaration does not declare anything tristanm Programming 5 10-24-2005 04:00 PM
How to declare a defaul argument in my function? bigapple Programming 3 08-11-2005 10:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration