Pass Filename to another function

This is a discussion on Pass Filename to another function within the C Programming forums, part of the General Programming Boards category; I'm trying to pass a filename to another function called first. But I keep getting the error "char *[50] not ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    9

    Post Pass Filename to another function

    I'm trying to pass a filename to another function called first. But I keep getting the error "char *[50] not compatible with char *". The error is in the line "filename = strtok(....)". But I don't know how to fix it.

    This is just a section of a function called main:

    Code:
    cmdptr =  strtok( cmdline, " " );		//retrieves first word/token in command line
    		
    if ( strcmp(cmdptr ,firststring) == 0) {   //firststring is just a string used to compare
    			
    			numptr = strtok( NULL, " ");	  //retrieves num
    
    			num = atoi(numptr);	//converts string to int	
    				
       /*ERROR HERE BUT WHY?*/ filename = strtok( NULL, " "); //retrieves filename
    						
    			first( num, filename );			//execute FIRST function
    				
    }

    filename is declared as " char *filename[50]; " as a global.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, char *filename[50] is a really bad declaration for a filename (it gives you 50 pointers to places you could put a filename, but no actual space for one). Maybe you just want char filename[50] -- that would give you one string of up to 49 characters.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    still doesn't work

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    I know that strtok returns a "char *"....

    it just won't work

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by awesmesk8er View Post
    I know that strtok returns a "char *"....

    it just won't work
    You're going to have to be more specific than that.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    strtok is a function in the <string.h> library

    the function looks like this:

    char *strtok( char *s1, const char *s2);

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes. That is true. And the function works fine, 'cause we've all used it. So -- what (specifically) doesn't work?

    Edit: Oh, and regardless of type, you can't assign an array as a whole. You probably want strcpy(filename, strtok(whatever)).

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    when I set
    filename = strtok( NULL, " ")

    it gives me the error: Operands of = have incompatible types 'char * [50]' and 'char *'.
    and Lvalue required.

    but I want filename to be of type "char * [50]" so I can pass it to a function of that type "char *[ ]"

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you want filename to be of type char *[50] you can do that -- now you just have to decide which of the fifty char *'s you want to assign to. Do you want
    Code:
    filename[0] = strtok(whatever);
    or
    Code:
    filename[1] = strtok(whatever);
    or
    .
    .
    .
    or
    Code:
    filename[49] = strtok(whatever);
    You have to pick a slot to put it in.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    9
    oh ok.

    thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21