Thread: write a function to retern char **

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    write a function to retern char **

    Hi All, am trying to write a function that returns a pointer to a character pointer
    something of the form
    Code:
    char  ** get_command(int loc);
    but I get an error at the prototype definition that * is unexpected!
    Is it that functions in C cannot return char ** type or do i need to define my function differently?

    Help is highly appreciated.
    sismail

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57
    In C, we can declare function with return type char **.
    It won't give the error if you returned the pointer to a character pointer in a function.
    I just tried some program. It won't be a useful code for you. But in that it return char **. it doesn't give any error.
    Code:
    #include<stdio.h>
    char **func(char **a);
    
    int main(int argc, char **argv)
    {
    char **av=func(argv);
    printf("%s\n",av[0]);
    return 0;
    }
    char **func(char **a)
    {
            return a;
    }
    I think the problem may be in your program. Post your code . Then only, I can solve your problem.
    Last edited by sganesh; 03-06-2010 at 03:49 AM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    I am using GCC V4.1.2
    Code:
    char ** getcommand(int loc);
    This prototype says no error
    As ganesh said problem may be in your code, other wise paste the error message here!
    but Im know for sure that C can return pointer to pointer to char

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Here is the code .. By the way am using Minix cc compiler. Every thing was working just fine untill i added the function

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <errno.h>
    #include <string.h> 
    #include <fcntl.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    
    extern char **getline();
    char **getNextCommand(Char **args,int order,int npipes);/* error at this line */
    int main() {
      int i,j, npipes,status;
      pid_t pid , npid;
      char **args, **new_args, **args_temp;
      int * pipe_fd;
      while(1) {
          /* Some code here ... */
         if( (args = getNextCommand(args,j,npipes)) == NULL){
              fprintf(stderr, "Un expected error\n");
             exit(2);
          }
       /* Some code here ... */
    }
    
    char ** getNextCommand(Char **args,int order,int npipes)
    {
    	int j,i,count;
    	char ** new_args;
    	if(order == 0){
    		j =0;
    		for( i=0;args[i] != NULL && strcmp(args[i],"|") != 0 ; i++)
    			new_args[j++] = args[i];
    		new_args[i] = NULL;	
    	}
    	else{
    		count = 0; i =0; j =0;
    		while(args[i] != NULL && strcmp(args[i] , "|") == 0){
    			count ++; i++;
    		}/* end: while*/
    		
    		if(args[i] == NULL){ 
                                         fprintf(stderr, "command not properly formatted");
                                         return NULL;}
    		while(strcmp(args[i], "|") != 0 && args[i] != NULL){
    			new_args[j++] = args[i++];
    		}/* end: while */
    		new_args[j] =NULL;	
    	}/* end else */
    
    	return new_args;
    }/* end getNextCommand */
    Last edited by sismail; 03-06-2010 at 03:51 AM.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: write a function to retern char **

    It may be the problem.
    I think you are using character type as Char rather than char.
    while declaring and defining getNextCommand function

    And I am using cc compiler.
    Last edited by sganesh; 03-06-2010 at 03:56 AM.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    66

    Thumbs up Solution

    This code works fine for me
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <errno.h>
    #include <string.h>
    #include <fcntl.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    
    extern char **getline();
    char **getNextCommand(char **args,int order,int npipes);/* error at this line */
    int main() {
      int i,j, npipes,status;
      pid_t pid , npid;
      char **args, **new_args, **args_temp;
      int * pipe_fd;
      while(1) {
          /* Some code here ... */
         if( (args = getNextCommand(args,j,npipes)) == NULL){
              fprintf(stderr, "Un expected error\n");
             exit(2);
          }
       /* Some code here ... */
      }
    }
    
    char ** getNextCommand(char **args,int order,int npipes)
    {
            int j,i,count;
            char ** new_args;
            if(order == 0){
                    j =0;
                    for( i=0;args[i] != NULL && strcmp(args[i],"|") != 0 ; i++)
                            new_args[j++] = args[i];
                    new_args[i] = NULL;
            }
            else{
                    count = 0; i =0; j =0;
                    while(args[i] != NULL && strcmp(args[i] , "|") == 0){
                            count ++; i++;
                    }/* end: while*/
    
                    if(args[i] == NULL){
                                         fprintf(stderr, "command not properly formatted");
                                         return NULL;}
                    while(strcmp(args[i], "|") != 0 && args[i] != NULL){
                            new_args[j++] = args[i++];
                    }/* end: while */
                    new_args[j] =NULL;
            }/* end else */
    
            return new_args;
    }/* end getNextCommand */
    I think you must change Char as char.
    I run the command like
    $ a.out ls| find and it was working fine!!

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    Thanks a lot that was the problem. Changed Char to char and all is good.
    Appreciate the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The UNIX System Interface
    By rrc55 in forum C Programming
    Replies: 1
    Last Post: 10-20-2009, 05:56 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

Tags for this Thread