Simple function problem

This is a discussion on Simple function problem within the C Programming forums, part of the General Programming Boards category; Hello. I have um simple program that just count how many args are introduced by user. Example: insert 20 30 ...

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

    Question Simple function problem

    Hello.

    I have um simple program that just count how many args are introduced by user.

    Example: insert 20 30 should say 3 args. The problem is i can count the args correctly but one of my variavles changes and i don't want.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <readline/readline.h>
    #include <readline/history.h>
    #include <string.h>
    
    int num_args(char *op)
    {
    int number=0;
    char *temp;
    temp = strtok (op," ");
      while (temp != NULL)
      {
    number++;
    /*    printf ("%s\n",pch);*/
        temp = strtok (NULL, " ");
      }
      return number;
    }
    
    int main ()
    {
    char *r;
    int a;
    r=readline("LETRAS: ");
    a=num_args(r);
    printf("Number of ARGS: %d and comand:%s\n",a,r);
    }
    as you can see i want to keep r as it was after user insert the comand, but when i passed to function changes.
    Help me please....

    Tks
    HIT

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you don't want to change the string, strtok is not for you. Or alternatively you can create a copy of the command string and call strtok on that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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