473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing string parameter to funtion

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
13 2675
<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
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
"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
"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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
0
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
4
by: Pushkar Pradhan | last post by:
I want a function to execute another function, which I pass to it, sometimes it may be mm_6r6c_6r6c, mm_2r2c_2r2c, ... etc. thus this style. double exec_basecase(void (*func)(double *a, double...
7
by: Robert Lario | last post by:
For examples sake I have made up a very simple example. I have an object called foo1 which is of type foo. I want to be able to call a funtion called myfunc as follows: myfunc(ref foo1) ...
17
by: Kevin Blount | last post by:
I have a system that I want to try, which requires me, from an aspx page, to call a generic function to work perform one task, then i want to call another function with the results of that task. ...
5
by: Lee Xuzhang | last post by:
/* from SICP -- Exercise 4.21: ((lambda (n) ((lambda (fact) (fact fact n)) (lambda (ft k) (if (= k 1) 1 (* k (ft ft (- k 1))))))) 10) */
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
5
by: steven_orocos | last post by:
Hi, I'm tryin to pass a funtion as an argument in another funtion. This code works ---------------------------------- typedef void (*func)(int); void test1(int a){cout<<"1";} void...
1
by: shalabh6 | last post by:
Hi, A js file containing a function which is passing an alphanumeric string to another function in the same file, the second funtion requires 4 parameters to pass from the first function. The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.