By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

Passing string parameter to funtion

P: n/a
char word[50];

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

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

/* Print all words */
/* printf("%s\n",&word); */
/* What I want to do: Process all words, somethink like: */

process_word(word);

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

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(word[50]);
process_word(&word);
process_word(*word);
process_word(word);

Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char input[50]); is not working,
how to change the syntax to make it work.

Oct 19 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
<ma******@hotmail.comwrote in message news:
char word[50];

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

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

/* Print all words */
/* printf("%s\n",&word); */
/* What I want to do: Process all words, somethink like: */

process_word(word);

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

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(word[50]);
process_word(&word);
process_word(*word);
process_word(word);

Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char 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(word);
and
process_word("test");

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.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 19 '08 #2

P: n/a
ma******@hotmail.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)!=EOF)
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(word);
This is right, assuming that process_word("test") is
also right.
/* This format works passing parameters */
/* process_word("test"); */
}

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
es*****@ieee-dot-org.invalid
Oct 19 '08 #3

P: n/a
"Malcolm McLean" <re*******@btinternet.comwrites:
<ma******@hotmail.comwrote in message news:
>char word[50];
[...]
>/* This format works passing parameters */
/* process_word("test"); */
}

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(word[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(word);
This should have been ok. Are you sure you tried it?
>Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char 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(char input[50]);

really means this:

void processword(char *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(word);
and
process_word("test");

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_Keith) ks***@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"
Oct 19 '08 #4

P: n/a
"Malcolm McLean" <re*******@btinternet.comwrites:

<snip>
>I have tried:

process_word(word[50]);
process_word(&word);
process_word(*word);
process_word(word);

Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char 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(&word); would
not be an error.

--
Ben.
Oct 19 '08 #5

P: n/a
process_word("test");
process_word("test1");
process_word("test2");
-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(char *input);

introduced same way.
Oct 19 '08 #6

P: n/a
process_word("test");
process_word("test1");
process_word("test2");
-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(char *input);

introduced same way.
Oct 19 '08 #7

P: n/a
ma******@hotmail.com writes:
process_word("test");
process_word("test1");
process_word("test2");
-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(char *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(char *input)
{
/* ... */
}

and any calls should look like this:

process_word(word);

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_Keith) ks***@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"
Oct 19 '08 #8

P: n/a
On Oct 19, 6:28 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
<masso...@hotmail.comwrote in message news:
char word[50];
in = fopen("test.txt", "r");
while(fscanf(in,"%s",&word)!=EOF)
{
/* Print all words */
/* printf("%s\n",&word); */
/* What I want to do: Process all words, somethink like: */
process_word(word);
/* This format works passing parameters */
/* process_word("test"); */
}
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(word[50]);
process_word(&word);
process_word(*word);
process_word(word);
Function:
void processword(char input[50]);
So process_word("test"); to processword(char input[50]); works but
process_word(&word); to processword(char 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.
Oct 19 '08 #9

P: n/a
vipps...@gmail.com wrote:
"Malcolm McLean" <regniz...@btinternet.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
Oct 19 '08 #10

P: n/a
vi******@gmail.com writes:
On Oct 19, 6:28 pm, "Malcolm McLean" <regniz...@btinternet.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_Keith) ks***@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"
Oct 20 '08 #11

P: n/a
On Oct 20, 12:42 am, Peter Nilsson <ai...@acay.com.auwrote:
vipps...@gmail.com wrote:
"Malcolm McLean" <regniz...@btinternet.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));
Oct 21 '08 #12

P: n/a
ma******@hotmail.com wrote:
char word[50];

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

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

/* Print all words */
/* printf("%s\n",&word); */
/* What I want to do: Process all words, somethink like: */

process_word(word);

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

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(word[50]);
process_word(&word);
process_word(*word);
process_word(word);

Function:
void processword(char input[50]);

So process_word("test"); to processword(char input[50]); works but

process_word(&word); to processword(char 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?
Oct 21 '08 #13

P: n/a
Peter Nilsson <ai***@acay.com.auwrites:
vipps...@gmail.com wrote:
"Malcolm McLean" <regniz...@btinternet.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.

Nov 10 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.