I can't seem to figure out how to send a string as a paramenter to a
function.
I have this structure:
struct list {
char thread[25];
struct list *previous;
struct list *next;
};
struct list *test;
When I call this function I would like to give it a string (char array) as
argument that will be writtin to the thread field in the list struct:
void fill(char[] arg){
test->thread = arg;
}
But it seems that its not the way to do it, hope someone can help.
JS 20 1754
this is how it should be done ....
void fill(char arg[])
{
strcpy(test->thread,arg);
}
u may have to do - #include <string.h> if you are using a c++ compiler
..
JS wrote: I can't seem to figure out how to send a string as a paramenter to a function.
I have this structure:
struct list { char thread[25]; struct list *previous; struct list *next; };
struct list *test;
When I call this function I would like to give it a string (char
array) as argument that will be writtin to the thread field in the list struct:
void fill(char[] arg){
test->thread = arg;
}
But it seems that its not the way to do it, hope someone can help.
JS ra**********@gmail.com wrote: this is how it should be done ....
void fill(char arg[]) { strcpy(test->thread,arg); }
I have also tried to use char *arg and it also works fine...are there any
difference?
JS wrote: I can't seem to figure out how to send a string as a paramenter to a function.
I have this structure:
struct list { char thread[25]; struct list *previous; struct list *next; };
struct list *test;
When I call this function I would like to give it a string (char
array) as argument that will be writtin to the thread field in the list struct:
void fill(char[] arg){
test->thread = arg;
}
But it seems that its not the way to do it, hope someone can help.
JS
See http://www.eskimo.com/~scs/C-faq/q8.3.html
You probably want something like this (not compiled or tested code)
void fill(const char *arg)
{
/* if arg is NULL, deal somehow */
size_t len = strlen(arg);
if (len < sizeof(test->thread)) {
memmove(test->thread, arg, len+1);
}
else {
/* doesn't fit, deal */
}
}
I'm assuming you have allocated the list, as in
test = malloc(sizeof *test)?
-David
JS wrote: I can't seem to figure out how to send a string as a paramenter to a function.
I have this structure:
struct list { char thread[25]; struct list *previous; struct list *next; };
struct list *test;
When I call this function I would like to give it a string (char array) as argument that will be writtin to the thread field in the list struct:
void fill(char[] arg){
test->thread = arg;
}
You should use function strcpy, or, function strncpy to
be safe. Change the function fill, to one that dynamically
allocates a node and "fills" the string and places the
new node in the list. See function AddToList below.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define THRD_SZ 25
struct list
{
char thread[THRD_SZ+1];
struct list *previous;
struct list *next;
};
struct list *AddToList(struct list **head,const char *thread);
void PrintList(struct list *head);
void FreeList(struct list **head);
int main(void)
{
struct list *test = NULL;
AddToList(&test,"Capitol Hill");
AddToList(&test,"Senator Smith");
AddToList(&test,"Senator With A Long Name That "
"Seems To Never End");
puts("The Threads....");
PrintList(test);
FreeList(&test);
return 0;
}
struct list *AddToList(struct list **head,const char *thread)
{
struct list *tmp;
if((tmp = malloc(sizeof *tmp)) != NULL)
{
strncpy(tmp->thread,thread,THRD_SZ);
tmp->thread[THRD_SZ] = '\0';
tmp->previous = NULL;
tmp->next = *head;
*head = tmp;
}
return tmp;
}
void PrintList(struct list *head)
{
unsigned i;
for(i = 0 ; head; head = head->next,i++)
printf("%4u) %s\n",i+1,head->thread);
return;
}
void FreeList(struct list **head)
{
struct list *tmp;
for( ; *head; *head = tmp)
{
tmp = (*head)->next;
free(*head);
}
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
On Thu, 21 Apr 2005 16:30:48 +0200, JS wrote: ra**********@gmail.com wrote:
this is how it should be done ....
void fill(char arg[]) { strcpy(test->thread,arg); }
I have also tried to use char *arg and it also works fine...are there any difference?
void fill(char arg[]) is interpreted by the compiler sa if you had written
void fill(char *arg). Remember that you can't pass arrays to functions in
C. If you try to pass an array in the caller the normal conversion to a
pointer to the array's first element in performed, so a pointer gets
passed. The designers of C decided it was a good idea to mirror this
within the funciton parameter list.
Lawrence
On Thu, 21 Apr 2005 07:09:32 -0700, ramakrishnat wrote: this is how it should be done ....
void fill(char arg[]) { strcpy(test->thread,arg); }
u may have to do - #include <string.h> if you are using a c++ compiler
If you are compiling C code you should be using a C compiler. But note
that you should #include <string.h> if you use strcpy() in C too. SOme
compilers may not complain if you don't but that doesn't mean the code is
correct.
Lawrence
JS wrote: ra**********@gmail.com wrote:
this is how it should be done ....
void fill(char arg[]) { strcpy(test->thread,arg); } I have also tried to use char *arg and it also works fine...are there any difference?
There is no difference, but only in the context of a parameter
declaration, i.e.:
void f(char *stuff);
and
void f(char stuff[]);
are semantically indistinguishable.
HTH,
--ag
--
Artie Gold -- Austin, Texas http://it-matters.blogspot.com (new post 12/5) http://www.cafepress.com/goldsays
Al Bowers wrote: JS wrote: I can't seem to figure out how to send a string as a paramenter to
a function.
I have this structure:
struct list { char thread[25]; struct list *previous; struct list *next; };
struct list *test;
When I call this function I would like to give it a string (char
array) as argument that will be writtin to the thread field in the list
struct: void fill(char[] arg){
test->thread = arg;
}
You should use function strcpy, or, function strncpy to be safe.
YMMV, but I think strncpy is a basically useless function.
e.g. see http://www.eskimo.com/~scs/C-faq/q13.2.html
Which doesn't even mention the irritiating side effect of setting the
remainder of the string to the null character, a possible performance
problem if the target is much bigger than the source.
Using a combination of strlen and memcpy/memmove seems like more the
way to go when you aren't sure source fits into target (or strncat).
-David ra**********@gmail.com writes: this is how it should be done ....
void fill(char arg[]) { strcpy(test->thread,arg); }
u may have to do - #include <string.h> if you are using a c++ compiler
(Please don't top-post. Your reply belongs after, or interspersed
with, any quoted text, which should be trimmed to what's necessary to
provide enough context.)
(Please don't use abbreviations like 'u'; take the time to spell out
the word.)
You should have a "#include <string.h>" regardless of what compiler
you're using. A C90 compiler may not diagnose the error, but it's
still an error.
If you're using a C++ compiler, you should be programming in C++ and
posting to comp.lang.c++. C is compiled with C compilers.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Lawrence Kirby <lk****@netactive.co.uk> writes:
[...] void fill(char arg[]) is interpreted by the compiler sa if you had written void fill(char *arg). Remember that you can't pass arrays to functions in C. If you try to pass an array in the caller the normal conversion to a pointer to the array's first element in performed, so a pointer gets passed. The designers of C decided it was a good idea to mirror this within the funciton parameter list.
This was, in my opinion, a bad idea. There's enough confusion between
arrays and pointers; allowing "char arg[]" in a parameter list to mean
"char *arg" only adds to it.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
In article <ln************@nuthaus.mib.org>
Keith Thompson <ks***@mib.org> wrote: If you're using a C++ compiler, you should be programming in C++ and posting to comp.lang.c++. C is compiled with C compilers.
Indeed.
I have to wonder whether comp.lang.c++ is plagued with people
posting advice like "don't use new, class, templates, or exceptions
in case you want to compile your C++ code with a C compiler"... :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
On Thu, 21 Apr 2005 20:38:03 GMT, Keith Thompson <ks***@mib.org> wrote
in comp.lang.c: Lawrence Kirby <lk****@netactive.co.uk> writes: [...] void fill(char arg[]) is interpreted by the compiler sa if you had written void fill(char *arg). Remember that you can't pass arrays to functions in C. If you try to pass an array in the caller the normal conversion to a pointer to the array's first element in performed, so a pointer gets passed. The designers of C decided it was a good idea to mirror this within the funciton parameter list.
This was, in my opinion, a bad idea. There's enough confusion between arrays and pointers; allowing "char arg[]" in a parameter list to mean "char *arg" only adds to it.
Indeed it is, but it's 30 years too late to do anything about it.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
JS wrote on 21/04/05 : struct list { char thread[25]; struct list *previous; struct list *next; };
void fill(char[] arg){
test->thread = arg;
I feel rather bizarre that you pretend to manipulate complex concepts
like double linked lists without knowing the vary basics of the
language.
What the hell is your C-book ? Or do you think that beeing an expert in
some other language makes you an expert in C just by magic ?
C is not a kiddy language. It needs to be learnt from scratch, step by
step. It takes time. One of the step is about strings and strings
functions like strcpy(), strncat() etc.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"There are 10 types of people in the world today;
those that understand binary, and those that dont." ra**********@gmail.com wrote on 21/04/05 : this is how it should be done ....
void fill(char arg[]) { strcpy(test->thread,arg); }
u may have to do - #include <string.h> if you are using a c++ compiler
Nobody here knows what a 'c++ compiler' is, but yes, if you want to
properly use a function, its prototype must be in scope. Including the
dedicated header certainly is the best way of achieving this goal.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
JS wrote on 21/04/05 : void fill(char arg[]) { strcpy(test->thread,arg); }
I have also tried to use char *arg and it also works fine...are there any difference?
Thre is not difference. *In a parameter context*, 'type *id' and 'type
id[]' are identical.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
..sig under repair
Chris Torek wrote on 21/04/05 : In article <ln************@nuthaus.mib.org> Keith Thompson <ks***@mib.org> wrote: If you're using a C++ compiler, you should be programming in C++ and posting to comp.lang.c++. C is compiled with C compilers.
Indeed.
I have to wonder whether comp.lang.c++ is plagued with people posting advice like "don't use new, class, templates, or exceptions in case you want to compile your C++ code with a C compiler"... :-)
LOL !
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
Joe Wright wrote on 01/05/05 : Emmanuel Delahaye wrote: Note that memmove() is expansive. Only needed when there is overlapping. Dolly Parton's bodice is expansive. A drink at Crazy Horse is expensive.
Lol! Well noticed.
I love Paris. Tell me again when the French leave town. July? :=)
The good period is mid-July to mid-August.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J.R. |
last post by:
Hi folks,
The python can only support passing value in function call (right?), I'm
wondering how to effectively pass a large parameter, such as a large list or
dictionary?
It could achieved...
|
by: N. Demos |
last post by:
How do you dynamically assign a function to an element's event with
specific parameters?
I know that the code is different for MSIE and Mozilla, and need to know
how to do this for both.
I...
|
by: Grumble |
last post by:
Hello,
I have the following structure:
struct foo {
char *format; /* format string to be used with printf() */
int nparm; /* number of %d specifiers in the format string */
/* 0 <= nparm <=...
|
by: Adam Hartshorne |
last post by:
What is named parameter mechanism? Any ideas? I am looking through some
code and there is a comment saying "VC++ has trouble with the named
parameters mechanism", which i have no idea what this...
|
by: mayur_hirpara |
last post by:
Hi,
I am looking to call a javascript function using variable number of
parameters.
Suppose I have a function
foo(param)
{
.....
}
I want it to be called unpredictebly when certain action...
|
by: John Friedland |
last post by:
My problem: I need to call (from C code) an arbitrary C library
function, but I don't know until runtime what the function name is,
how many parameters are required, and what the parameters are. I...
|
by: arne |
last post by:
Hi all,
imagine I call a function, but omit one of the parameters, like:
foo.c:
void foo( int a, int b ) {
/* do something with a and b */
return;
}
|
by: Xah Lee |
last post by:
In this article, i explain how the use of bit masks is a hack in many
imperative languages.
Often, a function will need to take many True/False parameters. For
example, suppose i have a function...
|
by: rh0dium |
last post by:
Hi all,
Below is a basic threading program. The basic I idea is that I have a
function which needs to be run using a queue of data. Early on I
specified my function needed to only accept basic...
|
by: Googy |
last post by:
Hi friends!!
As we know that the input parameters in a function is fixed when the
function is defined but how does printf processes variable number of
input arguments ?
For example:
1....
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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
|
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...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |