Thread: String issue

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    String issue

    I am always returning junk values to cmdLine in main.
    I have tried declaring cmdLine as: char * cmdLine

    I also tried with the current code sending *cmdLine but then i get a pointer from int without cast error.
    I want to send the char pointer to the sub-routine, have the sub routine put the string at that address and exit leaving me with a variable with a string in it in main. This is a re-occurring issue in code that I write.

    Thanks!

    Code:
    const char *builtInCmd[3] = {
    		"cd",
    		"exit",
    		"help"}; 
    int main (int argc, char **argv)
    {
    // Local Variables
    	int childPid, isBuiltIn;
      char cmdLine[50];
      if(readCommandLine(cmdLine) == 1) {    
    			printf("The command is %s\n", cmdLine);
    
    			isBuiltIn = isBuiltInCommand(cmdLine);
    			printf("The builtin Numb is:%i", isBuiltIn);
    		}
    }
    
    //Read user input and tare it down
    int readCommandLine(char *cmd_p){
    	char buf[80], *p;
    	fflush (stdout);
    	cmd_p = fgets (buf, 80, stdin);
    	if(strlen(cmd_p) > 0)
    		return 1;
    
    	return -1;
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    cmd_p = fgets (buf, 80, stdin);
    You're passing cmd_p into your function, but you're reading into buf. Of course you're not getting the data you want.
    "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

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    fgets() returns buf when it is done though. Unless it is the end of file then it returns a null char.
    That being the case, I still don't understand.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That just changes the local value of cmd_p. It's not going to actually put anything into the area where cmd_p originally pointed, and the changes to cmd_p will also not be seen outside the function.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Just replace buf with cmd_p and forget about the fgets() return value.
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM