Passing string parameter to funtion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • masso600@hotmail.com

    Passing string parameter to funtion

    char word[50];

    in = fopen("test.txt ", "r");

    while(fscanf(in ,"%s",&word)!=E OF)
    {

    /* Print all words */
    /* printf("%s\n",& word); */


    /* What I want to do: Process all words, somethink like: */

    process_word(wo rd);

    /* This format works passing parameters */
    /* process_word("t est"); */
    }

    Well this does not work, I cant get word-variable to function in right
    format. How do I pass word-variable to function process_word().

    I have tried:

    process_word(wo rd[50]);
    process_word(&w ord);
    process_word(*w ord);
    process_word(wo rd);

    Function:
    void processword(cha r input[50]);

    So process_word("t est"); to processword(cha r input[50]); works but

    process_word(&w ord); to processword(cha r input[50]); is not working,
    how to change the syntax to make it work.

  • Malcolm McLean

    #2
    Re: Passing string parameter to funtion

    <masso600@hotma il.comwrote in message news:
    char word[50];
    >
    in = fopen("test.txt ", "r");
    >
    while(fscanf(in ,"%s",&word)!=E OF)
    {
    >
    /* Print all words */
    /* printf("%s\n",& word); */
    >
    >
    /* What I want to do: Process all words, somethink like: */
    >
    process_word(wo rd);
    >
    /* This format works passing parameters */
    /* process_word("t est"); */
    }
    >
    Well this does not work, I cant get word-variable to function in right
    format. How do I pass word-variable to function process_word().
    >
    I have tried:
    >
    process_word(wo rd[50]);
    process_word(&w ord);
    process_word(*w ord);
    process_word(wo rd);
    >
    Function:
    void processword(cha r input[50]);
    >
    So process_word("t est"); to processword(cha r input[50]); works but
    >
    process_word(&w ord); to processword(cha r input[50]); is not working,
    how to change the syntax to make it work.
    >
    The address of operator is redundant for an array. (That's just one of the
    funny quirks of C).
    process_word(wo rd);
    and
    process_word("t est");

    are both valid ways of calling the function with a char * argument. If the
    first doesn't work, there must be something wrong with the data stored in
    the array. Your input-reading code is a bit crude, however you are printing
    out the values. Are they correct? Try putting asterisks between the string
    to make sure there are no stray spaces.
    You should also use while( fscanf(..) == 1) as the test condition, not EOF.
    The function returns the number of fields read correctly.

    --
    Free games and programming goodies.


    Comment

    • Eric Sosman

      #3
      Re: Passing string parameter to funtion

      masso600@hotmai l.com wrote:
      char word[50];
      >
      in = fopen("test.txt ", "r");
      It would be a good idea to check whether the fopen()
      succeeded or failed ...
      while(fscanf(in ,"%s",&word)!=E OF)
      Get rid of the `&'. See Questions 6.12 and 12.12b in
      the comp.lang.c Frequently Asked Questions (FAQ) list at
      <http://www.c-faq.com/to understand why. Also see
      Question 12.20 (and its footnote) for why an unrestricted
      "%s" is dangerous.
      {
      >
      /* Print all words */
      /* printf("%s\n",& word); */
      If you un-comment this, get rid of the `&'.
      >
      >
      /* What I want to do: Process all words, somethink like: */
      >
      process_word(wo rd);
      This is right, assuming that process_word("t est") is
      also right.
      /* This format works passing parameters */
      /* process_word("t est"); */
      }
      >
      Well this does not work, I cant get word-variable to function in right
      format. How do I pass word-variable to function process_word().
      What do you mean by "does not work?"

      --
      Eric Sosman
      esosman@ieee-dot-org.invalid

      Comment

      • Keith Thompson

        #4
        Re: Passing string parameter to funtion

        "Malcolm McLean" <regniztar@btin ternet.comwrite s:
        <masso600@hotma il.comwrote in message news:
        >char word[50];
        [...]
        >/* This format works passing parameters */
        >/* process_word("t est"); */
        >}
        >>
        >Well this does not work, I cant get word-variable to function in right
        >format. How do I pass word-variable to function process_word().
        >>
        >I have tried:
        >>
        >process_word(w ord[50]);
        This tries to pass a value of type char. Since it's the value of a
        nonexistent object (the element just past the end of your array), it
        would be wrong even if it were of the correct type.
        >process_word(& word);
        Nope.
        >process_word(* word);
        Nope.
        >process_word(w ord);
        This should have been ok. Are you sure you tried it?
        >Function:
        >void processword(cha r input[50]);
        >>
        >So process_word("t est"); to processword(cha r input[50]); works but
        >>
        >process_word(& word); to processword(cha r input[50]); is not working,
        >how to change the syntax to make it work.
        You should read section 6 of the comp.lang.c FAQ.

        A function can't really have a parameter of array type. If you try to
        declare such a parameter, it's really of pointer type. So this
        declaration:

        void processword(cha r input[50]);

        really means this:

        void processword(cha r *input);

        Note that the "50" is completely ignored. (This is one of my least
        favorite C language features.)
        The address of operator is redundant for an array. (That's just one of the
        funny quirks of C).
        No, the "&" operator is not redundant for an array. If word is an
        array (as it is here, declared as "char word[50];"), then &word is the
        address of the entire array, an expression of type "char(*)[50]".

        The expression "word", in most contexts, evaluates to the address of
        the first element of the array, of type char*. As it happens, this is
        useful much more often than the address of the array as a whole; the
        usual way to deal with an array is via a pointer to its first element.
        Note that you need to have some additional mechanism to indicate the
        length of the array, or of the portion of it you're interested in.
        process_word(wo rd);
        and
        process_word("t est");
        >
        are both valid ways of calling the function with a char * argument. If the
        first doesn't work, there must be something wrong with the data stored in
        the array.
        If the first doesn't work, you need to be very clear about what
        "doesn't work" means. What happened when you tried it? What did you
        expect to happen? How do these differ?
        Your input-reading code is a bit crude, however you are printing
        out the values. Are they correct? Try putting asterisks between the string
        to make sure there are no stray spaces.
        You should also use while( fscanf(..) == 1) as the test condition, not EOF.
        The function returns the number of fields read correctly.
        fscanf does return EOF for certain kinds of error. It can also return
        0 in some cases. Yes, if you're reading a single item, checking the
        result against 1 makes sense; if you get something other than 1, you
        might then want to take some other action depending on whether it
        returned 0 or EOF.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        Nokia
        "We must do something. This is something. Therefore, we must do this."
        -- Antony Jay and Jonathan Lynn, "Yes Minister"

        Comment

        • Ben Bacarisse

          #5
          Re: Passing string parameter to funtion

          "Malcolm McLean" <regniztar@btin ternet.comwrite s:

          <snip>
          >I have tried:
          >>
          >process_word(w ord[50]);
          >process_word(& word);
          >process_word(* word);
          >process_word(w ord);
          >>
          >Function:
          >void processword(cha r input[50]);
          >>
          >So process_word("t est"); to processword(cha r input[50]); works but
          >>
          >process_word(& word); to processword(cha r input[50]); is not working,
          >how to change the syntax to make it work.
          >>
          The address of operator is redundant for an array. (That's just one of
          the funny quirks of C).
          I can't see what you mean by "redundant" here. If you need to take
          the address of an array, what else can you do but use &? If you mean
          redundant in the sense of meaningless, then process_word(&w ord); would
          not be an error.

          --
          Ben.

          Comment

          • masso600@hotmail.com

            #6
            Re: Passing string parameter to funtion

            process_word("t est");
            process_word("t est1");
            process_word("t est2");
            -tells that function is working

            printf("%s\n",& word);
            -tells that loop have access to every word on file individually

            So now, when I try to combine these features to call function on every
            word on file, it fails either on syntax error or if it get past
            compiler function does not work properly.

            I figured that easiest way to make it work is make my function work
            like printf();

            So I need to call inside loop

            process_word("% s\n",&word);

            Yet need to be solved how printf() is described and change

            void processword(cha r *input);

            introduced same way.

            Comment

            • masso600@hotmail.com

              #7
              Re: Passing string parameter to funtion

              process_word("t est");
              process_word("t est1");
              process_word("t est2");
              -tells that function is working

              printf("%s\n",& word);
              -tells that loop have access to every word on file individually

              So now, when I try to combine these features to call function on every
              word on file, it fails either on syntax error or if it get past
              compiler function does not work properly.

              I figured that easiest way to make it work is make my function work
              like printf();

              So I need to call inside loop

              process_word("% s\n",&word);

              Yet need to be solved how printf() is described and change

              void processword(cha r *input);

              introduced same way.

              Comment

              • Keith Thompson

                #8
                Re: Passing string parameter to funtion

                masso600@hotmai l.com writes:
                process_word("t est");
                process_word("t est1");
                process_word("t est2");
                -tells that function is working
                You didn't quote any context from the previous articles in this
                thread. Someone reading your latest article without seeing the rest
                of the thread isn't going to have any idea what process_word is
                supposed to do, or how "word" is declared. Please quote enough
                context so that your article makes sense on its own.
                printf("%s\n",& word);
                -tells that loop have access to every word on file individually
                From your previous description, word is declared as
                char word[50];
                so &word is a pointer to array of char, of type "char(*)[50]".
                printf's "%s" format expects a char*, not a pointer to an array. The
                above is very likely to work anyway, but the correct call is
                printf("%s\n", word);
                (This assumes that word contains a string, i.e., a sequence of
                characters terminated by '\0'.)
                So now, when I try to combine these features to call function on every
                word on file, it fails either on syntax error or if it get past
                compiler function does not work properly.
                So some things you tried resulted in syntax errors (that you haven't
                shown us), and other things you tried resulted in code that "does not
                work properly" in some manner that you haven't bothered to tell us,
                and you haven't told us which is which.

                If you show us your actual code, we can be of more help. By "actual
                code", I mean a complete compilable and executable program.
                Copy-and-paste the exact source file that you fed to the compiler;
                don't try to re-type it. Tell us what it does, what you expected it
                to do, and how those two things differ.
                I figured that easiest way to make it work is make my function work
                like printf();
                >
                So I need to call inside loop
                >
                process_word("% s\n",&word);
                >
                Yet need to be solved how printf() is described and change
                >
                void processword(cha r *input);
                >
                introduced same way.
                Based on what you told us so far, I seriously doubt that making your
                process_word function behave like printf is going to be useful.
                printf uses a fairly complicated mechanism that allows it to handle
                variable numbers of arguments of various types. In every example
                you've shown us so far, processword processes a single string.

                So, given that you've declared "word" as:

                char word[50];

                I'm reasonably sure that your process_word function needs to look like
                this:

                void process_word(ch ar *input)
                {
                /* ... */
                }

                and any calls should look like this:

                process_word(wo rd);

                Although "word" is declared as an array, you're actually passing a
                pointer to process_word. Read section 6 of the comp.lang.c FAQ,
                <http://www.c-faq.com/>, to understand what's going on. Please read
                and try to understand the entire section. C's treatment of pointers
                and arrays can be very confusing, but the underlying principles are
                fairly straightforward ; section 6 of the clc FAQ does an excellent job
                of explaining them.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                Nokia
                "We must do something. This is something. Therefore, we must do this."
                -- Antony Jay and Jonathan Lynn, "Yes Minister"

                Comment

                • vippstar@gmail.com

                  #9
                  Re: Passing string parameter to funtion

                  On Oct 19, 6:28 pm, "Malcolm McLean" <regniz...@btin ternet.comwrote :
                  <masso...@hotma il.comwrote in message news:
                  char word[50];
                  >
                  in = fopen("test.txt ", "r");
                  >
                  while(fscanf(in ,"%s",&word)!=E OF)
                  {
                  >
                  /* Print all words */
                  /* printf("%s\n",& word); */
                  >
                  /* What I want to do: Process all words, somethink like: */
                  >
                  process_word(wo rd);
                  >
                  /* This format works passing parameters */
                  /* process_word("t est"); */
                  }
                  >
                  Well this does not work, I cant get word-variable to function in right
                  format. How do I pass word-variable to function process_word().
                  >
                  I have tried:
                  >
                  process_word(wo rd[50]);
                  process_word(&w ord);
                  process_word(*w ord);
                  process_word(wo rd);
                  >
                  Function:
                  void processword(cha r input[50]);
                  >
                  So process_word("t est"); to processword(cha r input[50]); works but
                  >
                  process_word(&w ord); to processword(cha r input[50]); is not working,
                  how to change the syntax to make it work.
                  >
                  The address of operator is redundant for an array. (That's just one of the
                  funny quirks of C).
                  No it's not, &word is different than word.

                  with char word[50];, it's not guaranteed that sizeof word == sizeof
                  &word, nor that the pointers will have the same representation.
                  Also, word + 1, &word + 1 are different. I can't imagine why you'd
                  call it redundant.

                  Comment

                  • Peter Nilsson

                    #10
                    Re: Passing string parameter to funtion

                    vipps...@gmail. com wrote:
                    "Malcolm McLean" <regniz...@btin ternet.comwrote :
                    ... The address of operator is redundant for an array.
                    (That's just one of the funny quirks of C).
                    >
                    No it's not, &word is different than word. with char
                    word[50];, it's not guaranteed that sizeof word ==
                    sizeof &word, nor that the pointers will have the
                    same representation. Also, word + 1, &word + 1 are
                    different. I can't imagine why you'd
                    call it redundant.
                    You've described the differences, now describe the
                    practical use of &word that makes it necessary in C.

                    --
                    Peter

                    Comment

                    • Keith Thompson

                      #11
                      Re: Passing string parameter to funtion

                      vippstar@gmail. com writes:
                      On Oct 19, 6:28 pm, "Malcolm McLean" <regniz...@btin ternet.comwrote :
                      [...]
                      >The address of operator is redundant for an array. (That's just one of the
                      >funny quirks of C).
                      >
                      No it's not, &word is different than word.
                      >
                      with char word[50];, it's not guaranteed that sizeof word == sizeof
                      &word, nor that the pointers will have the same representation.
                      Also, word + 1, &word + 1 are different. I can't imagine why you'd
                      call it redundant.
                      ``sizeof word'' is the size of the array, in this case 50. If you
                      want the size of the result of the expression ``word'', after the
                      conversion to a pointer, you can write ``sizeof (word+0)''.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      Nokia
                      "We must do something. This is something. Therefore, we must do this."
                      -- Antony Jay and Jonathan Lynn, "Yes Minister"

                      Comment

                      • vippstar@gmail.com

                        #12
                        Re: Passing string parameter to funtion

                        On Oct 20, 12:42 am, Peter Nilsson <ai...@acay.com .auwrote:
                        vipps...@gmail. com wrote:
                        "Malcolm McLean" <regniz...@btin ternet.comwrote :
                        ... The address of operator is redundant for an array.
                        (That's just one of the funny quirks of C).
                        >
                        No it's not, &word is different than word. with char
                        word[50];, it's not guaranteed that sizeof word ==
                        sizeof &word, nor that the pointers will have the
                        same representation. Also, word + 1, &word + 1 are
                        different. I can't imagine why you'd
                        call it redundant.
                        >
                        You've described the differences, now describe the
                        practical use of &word that makes it necessary in C.
                        You have never used a pointer to array in C?
                        Well, a quick one:

                        size_t f(size_t (*m)[42], size_t d) { size_t i, j, sum = 0; for(j = 0;
                        j < d; j++) for(i = 0; i < sizeof *m / sizeof **m; i++) sum += m[j]
                        [i]; return sum; }

                        /* ... */

                        size_t array[42] = { 4, 2 };
                        printf("sum %zu\n", f(&array, 1));

                        Comment

                        • Trent Josephsen

                          #13
                          Re: Passing string parameter to funtion

                          masso600@hotmai l.com wrote:
                          char word[50];
                          >
                          in = fopen("test.txt ", "r");
                          >
                          while(fscanf(in ,"%s",&word)!=E OF)
                          {
                          >
                          /* Print all words */
                          /* printf("%s\n",& word); */
                          >
                          >
                          /* What I want to do: Process all words, somethink like: */
                          >
                          process_word(wo rd);
                          >
                          /* This format works passing parameters */
                          /* process_word("t est"); */
                          }
                          >
                          Well this does not work, I cant get word-variable to function in right
                          format. How do I pass word-variable to function process_word().
                          >
                          I have tried:
                          >
                          process_word(wo rd[50]);
                          process_word(&w ord);
                          process_word(*w ord);
                          process_word(wo rd);
                          >
                          Function:
                          void processword(cha r input[50]);
                          >
                          So process_word("t est"); to processword(cha r input[50]); works but
                          >
                          process_word(&w ord); to processword(cha r input[50]); is not working,
                          how to change the syntax to make it work.
                          >
                          Umm, your function prototype has a different name than the function
                          you're using in the code. Is it process_word or processword?

                          Comment

                          • Tim Rentsch

                            #14
                            Re: Passing string parameter to funtion

                            Peter Nilsson <airia@acay.com .auwrites:
                            vipps...@gmail. com wrote:
                            "Malcolm McLean" <regniz...@btin ternet.comwrote :
                            ... The address of operator is redundant for an array.
                            (That's just one of the funny quirks of C).
                            No it's not, &word is different than word. with char
                            word[50];, it's not guaranteed that sizeof word ==
                            sizeof &word, nor that the pointers will have the
                            same representation. Also, word + 1, &word + 1 are
                            different. I can't imagine why you'd
                            call it redundant.
                            >
                            You've described the differences, now describe the
                            practical use of &word that makes it necessary in C.
                            Taking addresses of arrays is useful in code that operates on
                            multi-dimensional arrays. Most C programs don't use MDA so
                            using & on an array is correspondingly rare, but for those
                            programs that do using address-of-array is just what the
                            doctor ordered.

                            Comment

                            Working...