Passing string parameter to funtion

M

masso600

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.
 
K

Keith Thompson

Malcolm McLean said:
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.
Nope.


Nope.

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.
 
B

Ben Bacarisse

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.
 
M

masso600

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.
 
M

masso600

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.
 
K

Keith Thompson

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.
 
V

vippstar

char word[50];
in = fopen("test.txt", "r");

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

/* 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.
 
P

Peter Nilsson

Malcolm McLean said:
... 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.
 
K

Keith Thompson

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)''.
 
V

vippstar

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]
; return sum; }

/* ... */

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

Tim Rentsch

Peter Nilsson said:
Malcolm McLean said:
... 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top