#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Question Got stuck with pointers


    I am given an assignment to create my own shell. As a start i tried to write a code to parse input and print it. Here is my code

    #include <stdio.h>
    #include <string.h>

    parse(char *p)
    {

    char str[] ="I am creating my own shell";
    printf ("Splitting string \"%s\" into tokens:\n",str);
    *p = strtok (str," ");
    }
    print(char *c){
    while (*c != NULL) {
    printf ("%s\n",*c);
    *c = strtok (NULL, " ");
    }
    }
    main(){
    char * pch;
    parse(&pch);
    print(&pch);
    }

    I have no idea what are the errors. I am new to C language as well. Can anyone help me to correct this code please.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    You don't know what the errors are? You mean you haven't even tried to compile that mess yet? Well get off your lazy butt and compile it already! Then tell us what the error and warning messages are! Jeez! Do you expect us to do everything for you?

    And you'd better be sure to include the warnings! Warnings are much more important than error messages are! And never ignore warnings! If you get warnings, then your program is almost guaranteed to do something different from what you want it to do.

    And use code tags!; eg:
    Code:
        #include <stdio.h> 
        #include <string.h> 
        
         parse(char *p)
        {
        
          char str[] ="I am creating my own shell"; 
          printf ("Splitting string \"%s\" into tokens:\n",str); 
          *p = strtok (str," "); 
        }
        print(char *c){
        	while (*c != NULL) { 
           		 printf ("%s\n",*c); 
          		 *c = strtok (NULL, " "); 
          	} 
        }
        main(){
        	char * pch; 
        	parse(&pch);
        	print(&pch);
        }
    And learn to format proper. Even with the indentation restored that's an unreadable mess.

    Just a few things to start with:
    1. Functions always return something. Always declare the return type, especially if it's void. If you don't, then C assumes you meant int.

    2. When you declare a function to return something, which is what you have done, then return something! Don't make promises and then refuse to keep them!

    3. There is no such function as print, unless you had written it. If you meant printf, then read the documentation of printf to see how to use it.

    4. Don't attempt to call a function with a function prototype.

    5. When a function expects an argument to be a certain type, then give it an argument of that type. &pch is a char**, not a char*.

    6. Never try to force us to play guessing games with you. Give us all pertinent information, such as what error and warning messages you get.
    Last edited by dwise1_aol; April 19th, 2013 at 08:55 AM.

IMN logo majestic logo threadwatch logo seochat tools logo