Thread: function calling: basic doubt

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Post function calling: basic doubt

    Hi,

    I have a very basic doubt with calling a function,

    i have a function that accepts a string of arrays and returns a string of array as below:

    Code:
     char** parser(char **str)
        {
                 return str;
               }
    
    and i have the main calling it as :
    
    int main()
    {
        
          char label[50][25];
    
        
       	printf("enter expression");
    	scanf("%s",inputexp);
    	
        strcpy(label[0],inputexp);
                
    	   
        char** test=parser(label);
         
        }


    I am getting an error here.. can you please help me why.... i know it may be a very trivial case, but please do help me

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you get an error, surely you know what kind of error you are getting?

    At base, though, I'm guessing it's because label isn't a char** and can't be made into one.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Post

    The error is:

    cannot convert `char (*)[25]' to `char**' for argument `1' to `char** parser(char**)'

    how do I set it right, i mean i need to assign the first element of string of array with a string of my choice and then pass that string of array into the function which outputs another string of array, please help

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you want to pass that array, then do so. Your function then needs to take char[][25] and can't take a char**, since that's not what an array is.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    but the return type of the function cannot be an array right, i mean i need to return the array too, it can be done only with pointers right? correct me if iam wrong?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That is true, you cannot return an array. There is no way to make a pointer type out of a 2-D array, so either (1) you're not going to return an array -- pass it in instead or (2) you can't use "real" arrays, but instead you have to use "fake" arrays that you get from malloc.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    Post

    Iam Sorry i dint follow you, I have posted my code here, can you please suggest where exactly the problem is actually? i guess iam really confused with pointers and arrays now
    Last edited by doubty; 06-23-2009 at 01:34 AM.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    str is not a **. If you want a 2-D array out of your function, pass it in and work with it:
    Code:
    void function_name(char in_array[][25], char out_array[][25])

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I would go with tabstop's suggestion, but you can pass it in and return it as a pointer if you put it in a struct:
    Code:
    struct Array2D
    {
        char label[50][25];
    };
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Hi

    regarding creating a function with 2 arrays as the parameters, if you notice my function has a recursive call to it self, i.e to say that the substrings extracted by the function are stored back into the same array of string which are again used by the function to calculate subsub strings,

    can you give me pointers as to how this may be done. Thanks
    Last edited by doubty; 06-23-2009 at 12:03 AM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That's something completely different. Unless your wording is off. Does your function take two arrays, or a 2D array? Why do you feel you need to use recursion, and, how did you work out your logic on paper/in your head?

    Type up the logic of what it is you think you're trying to do, and we'll advise on that. I don't really see the point of us just writing up a function to do whatever it is you think you want to do and handing it out "just because".


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. A Function Calling a Function
    By dac in forum C++ Programming
    Replies: 8
    Last Post: 12-17-2006, 04:10 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM