0

i want to take the 1st argument from commandline, ie argv[1] and assign it to a char target[80]

i know im making some very stupid beginer mistakes... but just cant understand what :(

this is my code:

#include <stdio.h>
#include <string.h>
void converge(char *targ, char *src);
int main(char *argc[],char *argv[])
{
char target[80];
target=(*argv[1]);
converge(target, "This is a test of converge().");
printf("Final string: %s\n", target);
printf("%s",argv[1]);
return 0;

}
/* This function copies one string into another.
It copies characters to both the ends,
converging at the middle. */
void converge(char *targ, char *src)
{
int i, j;
printf("%s\n", targ);
for(i=0, j=strlen(src); i<=j; i++, j--) {
targ[i] = src[i];
targ[j] = src[j];
printf("%s\n", targ);
}
}

and all the problems are coming from this line target=(*argv[1]);

would be great if anyone could help on how this should work, and why this isnt working..
thanks in advance. :)
somjit

Edited by somjit{}

2
Contributors
2
Replies
5
Views
4 Years
Discussion Span
Last Post by somjit{}
1

Arrays are an aggregate type, you can't simply use the assignment operator. Copying is performed with the strcpy() function. argv[1] is also a string, so *argv[1] evaluates to a single character. I suspect you meant this:

strcpy(target, argv[1]);
0

deceptikonfacepalm :D

thankyou so much for the superfast reply @deceptikon :) i completely forgot about strcopy!!
thank you. problem solved :) u rock :)

Edited by somjit{}: just wanted to show my thanks :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.