How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code:
------------8<--------------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
allchars = argv[1];
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
------------------------------------------
execution:
------------8<--------------------------
[mox@home (/home/mox)]: ./cnsudo Hi this is a test
2) Adding: "this"
3) Adding: "is"
4) Adding: "a"
5) Adding: "test"
All: "Hi is a test"
------------------------------------------
How come I am missing the second argument ? But the strcat works ???
Thanks,
--Benoit Lefebvre be*************@gmail.com 13 1334
In article <11**********************@19g2000hsx.googlegroups. com>,
<be*************@gmail.comwrote:
>#include<stdio.h>
>int main (int argc, char *argv[]) {
int i;
int j;
char *allchars;
if (argv[1]) {
allchars = argv[1];
Here you are setting the pointer allchars to point to the same place
that argv[1] does. But you don't know how much space is allocated
at that location, so writing anything further at that location
is a bad idea.
You need to find a way to allocate as much memory as will be
required to put all of the arguments together, and then to copy
the arguments into that allocated block of memory.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton be*************@gmail.com wrote:
How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code:
------------8<--------------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
argv[1] points to the first argument after the program's invocation name,
which is in argv[0]. In some cases, the program may not receive an
invocation name.
allchars = argv[1];
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
allchars points to the first argument, i.e. argv[1]. You should not assume
that this array has enough space for you to concactenate the remaining
arguments. It has just enough elements to hold the first argument, plus one
more for a null character, no more.
You must initialise allchars to point to an array of chars that is
sufficient in length to hold a linear series of all the command-line
arguments. The total length of the arguments can be calculated by using
strlen on each one. Then allocate an array using malloc.
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
On Jul 27, 4:25 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1185567341.887707.278...@19g2000hsx.googlegroups. com>,
<benoit.lefeb...@gmail.comwrote:
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
allchars = argv[1];
Here you are setting the pointer allchars to point to the same place
that argv[1] does. But you don't know how much space is allocated
at that location, so writing anything further at that location
is a bad idea.
You need to find a way to allocate as much memory as will be
required to put all of the arguments together, and then to copy
the arguments into that allocated block of memory.
--
"It is important to remember that when it comes to law, computers
never make copies, only human beings make copies. Computers are given
commands, not permission. Only people can be given permission."
-- Brad Templeton
I'm not quite sure to understand at 100%..
How can I allocate the memory ?
Sorry for being a newbie ;)
--Benoit Lefebvre be*************@gmail.com
On Jul 27, 4:27 pm, santosh <santosh....@gmail.comwrote:
benoit.lefeb...@gmail.com wrote:
How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code:
------------8<--------------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
argv[1] points to the first argument after the program's invocation name,
which is in argv[0]. In some cases, the program may not receive an
invocation name.
allchars = argv[1];
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
allchars points to the first argument, i.e. argv[1]. You should not assume
that this array has enough space for you to concactenate the remaining
arguments. It has just enough elements to hold the first argument, plus one
more for a null character, no more.
You must initialise allchars to point to an array of chars that is
sufficient in length to hold a linear series of all the command-line
arguments. The total length of the arguments can be calculated by using
strlen on each one. Then allocate an array using malloc.
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
Oh.. I think I found how to fix it finally
--------8<----------------
if (argv[1]) {
strcat(allchars, argv[1]); <---------------------------
Here is the correction
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
printf("%i) Adding: \"%s\"\n",i, argv[i]);
}
}
}
--------------------------
Benoit Lefebvre wrote:
On Jul 27, 4:25 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:
>In article <1185567341.887707.278...@19g2000hsx.googlegroups. com>,
<benoit.lefeb...@gmail.comwrote:
>#include<stdio.h> int main (int argc, char *argv[]) {
int i;
int j;
char *allchars;
if (argv[1]) {
allchars = argv[1];
Here you are setting the pointer allchars to point to the same place that argv[1] does. But you don't know how much space is allocated at that location, so writing anything further at that location is a bad idea.
You need to find a way to allocate as much memory as will be required to put all of the arguments together, and then to copy the arguments into that allocated block of memory. -- "It is important to remember that when it comes to law, computers never make copies, only human beings make copies. Computers are given commands, not permission. Only people can be given permission." -- Brad Templeton
I'm not quite sure to understand at 100%..
How can I allocate the memory ?
Sorry for being a newbie ;)
No problem. To allocate memory during the runtime of the program, you can
use malloc or calloc or realloc. To start with malloc is simplest.
For example to allocate an array of 100 char objects you can do:
#include <stdlib.h>
#define ARR_SIZE 100
/* ... */
char *arr = NULL;
arr = malloc(ARR_SIZE);
if (arr == NULL) deal_with_failure();
else continue_program();
To allocate an array of 200 doubles, you can do:
/* ditto */
double *arr = NULL;
arr = malloc(ARR_SIZE * sizeof *arr);
/* ditto */
Notice that in C a char is 1 byte by definition, so in that char example a
size expression need not be included. The sizes of other C types can be
found by applying the sizeof operator to them.
Be sure to read this group's FAQ,
<http://www.c-faq.com/>
and wiki,
<http://www.clc-wiki.net/>
Benoit Lefebvre wrote:
On Jul 27, 4:27 pm, santosh <santosh....@gmail.comwrote:
> benoit.lefeb...@gmail.com wrote:
How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code:
------------8<--------------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
argv[1] points to the first argument after the program's invocation name, which is in argv[0]. In some cases, the program may not receive an invocation name.
allchars = argv[1];
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
allchars points to the first argument, i.e. argv[1]. You should not assume that this array has enough space for you to concactenate the remaining arguments. It has just enough elements to hold the first argument, plus one more for a null character, no more.
You must initialise allchars to point to an array of chars that is sufficient in length to hold a linear series of all the command-line arguments. The total length of the arguments can be calculated by using strlen on each one. Then allocate an array using malloc.
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
Oh.. I think I found how to fix it finally
--------8<----------------
if (argv[1]) {
strcat(allchars, argv[1]); <---------------------------
Here is the correction
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
printf("%i) Adding: \"%s\"\n",i, argv[i]);
}
}
}
--------------------------
Your correction has failed to correct anything. You still do not indicate
what allchars is pointing to.
In future, please post compilable code. Otherwise it becomes more difficult,
though not impossible, for us to help you.
On Jul 27, 4:43 pm, santosh <santosh....@gmail.comwrote:
Benoit Lefebvre wrote:
On Jul 27, 4:27 pm, santosh <santosh....@gmail.comwrote:
benoit.lefeb...@gmail.com wrote:
How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code:
------------8<--------------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
argv[1] points to the first argument after the program's invocation name,
which is in argv[0]. In some cases, the program may not receive an
invocation name.
allchars = argv[1];
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
allchars points to the first argument, i.e. argv[1]. You should not
assume that this array has enough space for you to concactenate the
remaining arguments. It has just enough elements to hold the first
argument, plus one more for a null character, no more.
You must initialise allchars to point to an array of chars that is
sufficient in length to hold a linear series of all the command-line
arguments. The total length of the arguments can be calculated by using
strlen on each one. Then allocate an array using malloc.
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
Oh.. I think I found how to fix it finally
--------8<----------------
if (argv[1]) {
strcat(allchars, argv[1]); <---------------------------
Here is the correction
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
printf("%i) Adding: \"%s\"\n",i, argv[i]);
}
}
}
--------------------------
Your correction has failed to correct anything. You still do not indicate
what allchars is pointing to.
In future, please post compilable code. Otherwise it becomes more difficult,
though not impossible, for us to help you.
Sorry.. Here is what I compiled when it worked.
--------- 8< ------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char * allchars;
allchars = (char*) malloc (1024);
if (argv[1]) {
strcat(allchars, argv[1]);
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
-------------------------------
Benoit Lefebvre wrote:
On Jul 27, 4:43 pm, santosh <santosh....@gmail.comwrote:
>Benoit Lefebvre wrote:
On Jul 27, 4:27 pm, santosh <santosh....@gmail.comwrote: benoit.lefeb...@gmail.com wrote:
How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code:
------------8<--------------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
>argv[1] points to the first argument after the program's invocation name, which is in argv[0]. In some cases, the program may not receive an invocation name.
allchars = argv[1];
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
>allchars points to the first argument, i.e. argv[1]. You should not assume that this array has enough space for you to concactenate the remaining arguments. It has just enough elements to hold the first argument, plus one more for a null character, no more.
>You must initialise allchars to point to an array of chars that is sufficient in length to hold a linear series of all the command-line arguments. The total length of the arguments can be calculated by using strlen on each one. Then allocate an array using malloc.
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
Oh.. I think I found how to fix it finally
--------8<----------------
if (argv[1]) {
strcat(allchars, argv[1]); <---------------------------
Here is the correction
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
printf("%i) Adding: \"%s\"\n",i, argv[i]);
}
}
}
--------------------------
Your correction has failed to correct anything. You still do not indicate what allchars is pointing to.
In future, please post compilable code. Otherwise it becomes more difficult, though not impossible, for us to help you.
Sorry.. Here is what I compiled when it worked.
--------- 8< ------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char * allchars;
allchars = (char*) malloc (1024);
In C you need not, and probably should not, cast the return value of malloc
and family. Doing so can prevent the compiler from warning you when you
fail to include stdlib.h, which is needed for the correct declaration of
malloc.
Indeed you _have_ failed to include stdlib.h, and thus calling a function
with no prototype in scope, you've invoked undefined behaviour.
Also instead of hard-coding a value for the array allocation, you might run
strlen over each program argument and add them all up to allocate just the
space you want. Don't forget to adjust the final length for any whitespace
you need and for the final null character.
if (argv[1]) {
strcat(allchars, argv[1]);
strcat and other string functions are declared in string.h, which you've not
included above.
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
}
The two IFs are not needed. Just loop from argv[1] to argv[argc-1].
}
}
printf("All: \"%s\"\n", allchars);
return 0;
}
-------------------------------
Also you never use the variable 'j'.
Benoit Lefebvre wrote On 07/27/07 16:50,:
[...]
char * allchars;
allchars = (char*) malloc (1024);
if (argv[1]) {
strcat(allchars, argv[1]);
Here's another problem, in addition to those that
santosh and Default User have pointed out. Both arguments
to strcat() must be strings, and a string is an array of
characters terminated by a char with the value zero. But
memory obtained from malloc() has unpredictable content:
it might be full of zeroes, it might be full of something
left over from an earlier use, it might be full of random
garbage. So you can't be sure that allchars is pointing
to a properly-formed string, as required by strcat().
Solution #1:
allchars[0] = '\0'; /* now allchars holds string */
strcat(allchars, argv[1]);
Solution #2 (better):
strcpy(allchars, argv[1]); /* different function */
-- Er*********@sun.com
Eric Sosman wrote:
Benoit Lefebvre wrote On 07/27/07 16:50,:
[...]
char * allchars;
allchars = (char*) malloc (1024);
if (argv[1]) {
strcat(allchars, argv[1]);
Here's another problem, in addition to those that
santosh and Default User have pointed out. Both arguments
to strcat() must be strings, and a string is an array of
characters terminated by a char with the value zero.
Yeah, good catch.
Brian
On Jul 28, 4:15 am, benoit.lefeb...@gmail.com wrote:
How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code:
------------8<--------------------------
#include<stdio.h>
int main (int argc, char *argv[])
{
int i;
int j;
char *allchars;
if (argv[1]) {
allchars = argv[1];
if (argv[2]) {
for (i = 2; i < argc; i++) {
printf("%i) Adding: \"%s\"\n",i, argv[i]);
strcat(allchars, " ");
strcat(allchars, argv[i]);
}
}
}
printf("All: \"%s\"\n", allchars);
return 0;}
------------------------------------------
execution:
------------8<--------------------------
[mox@home (/home/mox)]: ./cnsudo Hi this is a test
2) Adding: "this"
3) Adding: "is"
4) Adding: "a"
5) Adding: "test"
All: "Hi is a test"
------------------------------------------
How come I am missing the second argument ? But the strcat works ???
Thanks,
--Benoit Lefebvre
benoit.lefeb...@gmail.com
I suppose that all these args are stored in a consequentive memory,
which is, in this example,
the memory is something like this:
'H' 'i' '\0' 't' 'h' 'i' 's' '\0' 'i' 's' '\0' 'a' '\0' 't' 'e' 's'
't' '\0'.
and arg[1] is pointed to 'H', and arg[2] is pointed to 't', and so
on...
so when you encounted the statement: strcat(arg[1], " ");
we get
'H' 'i' ' ' '\0' 'h' 'i' 's' '\0' 'i' 's' '\0' 'a' '\0' 't' 'e' 's'
't' '\0'.
which now arg[2] is empty string now
I think this can explain the result.
-- L.H.
Harvey wrote:
On Jul 28, 4:15 am, benoit.lefeb...@gmail.com wrote:
>How can I join args from a function ?
Here is what I've done so far but I'm getting weird results
code: ------------8<-------------------------- #include<stdio.h>
int main (int argc, char *argv[]) { int i; int j; char *allchars;
if (argv[1]) { allchars = argv[1]; if (argv[2]) { for (i = 2; i < argc; i++) { printf("%i) Adding: \"%s\"\n",i, argv[i]); strcat(allchars, " "); strcat(allchars, argv[i]); } } }
printf("All: \"%s\"\n", allchars); return 0;}
------------------------------------------
execution: ------------8<-------------------------- [mox@home (/home/mox)]: ./cnsudo Hi this is a test 2) Adding: "this" 3) Adding: "is" 4) Adding: "a" 5) Adding: "test" All: "Hi is a test" ------------------------------------------
How come I am missing the second argument ? But the strcat works ???
I suppose that all these args are stored in a consequentive memory,
which is, in this example,
the memory is something like this:
'H' 'i' '\0' 't' 'h' 'i' 's' '\0' 'i' 's' '\0' 'a' '\0' 't' 'e' 's'
't' '\0'.
and arg[1] is pointed to 'H', and arg[2] is pointed to 't', and so
on...
This is not guaranteed in the C Standard. The memory layout is
implementation dependant. Also the idiomatic names are argv[0], argv[1]
etc.
so when you encounted the statement: strcat(arg[1], " ");
we get
'H' 'i' ' ' '\0' 'h' 'i' 's' '\0' 'i' 's' '\0' 'a' '\0' 't' 'e' 's'
't' '\0'.
which now arg[2] is empty string now
No we don't. argv[2] does not become an empty string.
"Harvey" <lu*********@gmail.comwrote in message
news:11*********************@d30g2000prg.googlegro ups.com...
I suppose that all these args are stored in a consequentive memory,
which is, in this example,
the memory is something like this:
'H' 'i' '\0' 't' 'h' 'i' 's' '\0' 'i' 's' '\0' 'a' '\0' 't' 'e' 's'
't' '\0'.
and arg[1] is pointed to 'H', and arg[2] is pointed to 't', and so
on...
so when you encounted the statement: strcat(arg[1], " ");
we get
'H' 'i' ' ' '\0' 'h' 'i' 's' '\0' 'i' 's' '\0' 'a' '\0' 't' 'e' 's'
't' '\0'.
which now arg[2] is empty string now
I think this can explain the result.
Putting the command line args in a contiguous block of memory is an obvious
thing to do. However you can't rely on this.
By calling strcat() you'd shift the characters down one - strictly it would
be undefined behaviour becuse the buffers are overlapping, but probably the
function is a simply byte copying loop. So argv[2] now points to "is", and
there is a double nul terminating it.
--
Free games and programming goodies. http://www.personal.leeds.ac.uk/~bgy1mm This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Pierre Fortin |
last post by:
This quest for understanding started very innocently...
A simple error on my part, passing on args as "args" instead of "*args" to
os.path.join() led me to wonder why an error wasn't raised...
...
|
by: Steven Bethard |
last post by:
So, as I understand it, in Python 3000, zip will basically be replaced
with izip, meaning that instead of returning a list, it will return an
iterator. This is great for situations like:
zip(*)...
|
by: TheDD |
last post by:
Hello all,
right now, i'm using the following macro to automatically add informations
to exceptions:
#define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args)
but AFAIK, it's...
|
by: Eric Sabine |
last post by:
Can someone give me a practical example of why I would join threads? I am
assuming that you would typically join a background thread with the UI
thread and not a background to a background, but...
|
by: Paul Czubilinski |
last post by:
Hello,
I would like to join few pdf files uploaded separetly into my website
into one downloable pdf file. Is it possible in php or is it neccessary
to download all these files one by one?
...
|
by: Joel Hedlund |
last post by:
Hi!
The question of type checking/enforcing has bothered me for a while, and
since this newsgroup has a wealth of competence subscribed to it, I
figured this would be a great way of learning...
|
by: Lloyd Zusman |
last post by:
I have a python-2.5 program running under linux in which I spawn a
number of threads. The main thread does nothing while these subsidiary
threads are running, and after they all complete, the main...
|
by: Supermansteel |
last post by:
I am joining these 2 tables together in Access 2003 and can't figure out the exact way of writing this script......Can anyone help?
I have the following SQL:
SELECT...
|
by: yuva |
last post by:
"Thank you for this amazing program. Within a weekend, my team
increased with 50 new people joining. Every time I logged on there was
a new affiliate. Absolutely amazing. "
S. Horvath SOUTH AFRICA...
|
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: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |