LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-26-2004, 02:24 PM   #1
Linh
Member
 
Registered: Apr 2003
Posts: 178

Rep: Reputation: 30
Problem with function declaration


C program in Linux
Problem with function declaration

=================
root:~# gcc -o function_1 function_1.c

function_1.c: In function `function_1':
function_1.c:12: parse error before `char'
function_1.c:17: warning: passing arg 1 of `strncpy' makes pointer from integer without a cast

--------------------------------------------------------

Code:
#include <stdio.h>         
#include <stdlib.h>       
#include <string.h>       

char function_1 (array_octal, process_name)
 char array_octal[112], char process_name[40];

 {
   char string_abc[12]  = "hello there";

   strncpy (process_name, string_abc, 12);
   return process_name;

 }

main ()

 {
   char array_octal_var1[112];
   char process_name_var1[40];

   function_1 (array_octal_var1, process_name_var1);
   printf ("process_name_var1 = %s", process_name_var1);
 }

Last edited by Linh; 04-26-2004 at 02:27 PM.
 
Old 04-26-2004, 03:16 PM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
You need a semi-colon after char array_octal[112].

Also, arrays are always passed by pointer in C/C++, so specifying a size for a char array parameter does nothing. Instead, you should either use:

Code:
char function_1 (array_octal, process_name)
 char array_octal[]; // Bold didn't work in code tags.. right here
 char process_name[];
 {
   char string_abc[12]  = "hello there";

   strncpy (process_name, string_abc, 12);
   return process_name;

 }
Or:

Code:
char function_1 (array_octal, process_name)
 char *array_octal; // Bold didn't work in code tags.. right here
 char *process_name;
 {
   char string_abc[12]  = "hello there";

   strncpy (process_name, string_abc, 12);
   return process_name;

 }

Last edited by deiussum; 04-26-2004 at 04:59 PM.
 
Old 04-26-2004, 04:48 PM   #3
Linh
Member
 
Registered: Apr 2003
Posts: 178

Original Poster
Rep: Reputation: 30
reply to deiussum

Thank you deiussum for your help.

Here is where all the confusion comes in with regard to syntax.
I tried the code below and it also works. I have not tried the two solutions that you have suggested though.

Code:
char*  function_1 (char array_octal [], char process_name [])

 {
   char string_abc[12]  = "hello there";

   strncpy (process_name, string_abc, 12);
   return process_name;

 }
 
Old 04-26-2004, 04:58 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Yeah, that is also a valid syntax, and actually is generally preferred for most things. I think the GNU coding standards generally prefer the "old" C-style declarations you had before.

Anyway, the problem you had before is that you had a comma, where you needed a semi-colon. I'll go back to my old post and put that semi-colon in bold for you, so you can see where your previous syntax was messed up.
 
  


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
warning: implicit declaration of function liguorir Linux - Software 5 10-22-2012 03:20 PM
implicit declaration of function 'getdelim' MicahCarrick Programming 2 05-02-2005 11:03 AM
Array declaration in class or main function??? redhatrosh Programming 4 03-15-2005 02:13 PM
Unintelligible declaration of signal function Nerox Programming 4 08-11-2004 04:45 PM
Arguments in PHP function declaration Parksy Programming 5 07-04-2004 04:22 PM

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

All times are GMT -5. The time now is 10:30 PM.

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