How to copy char array to char array?

gohgss's Avatar, Join Date: Mar 2007
Newbie Member
int main(int arg, char *argv [])
{

char *a[256];

// how we copy the array below
a[0]=argv[0];


return 0;
}
DaWei's Avatar, Join Date: Dec 2006
Team Leader
"char *array [256] declares an array of 256 char pointers. You have made no provisions for storage of actual characters, only the pointers. It is rather futile to duplicate argv in a local copy, since it is quite manipulable as it stands, even to the point of extending the lengths of strings to which it points.

Despite all that, you might want to copy argv, or copy the items which argv references. You need to tell us which of those things you want.

If you don't understand pointers, then refer to the link in my signature. Pointers are like entries in your address book; your girlfriend, or your data, does not live in your address book. They live at the location INDICATED in your address book. That location must exist, and be populated, or your address book isn't worth chit.
gohgss's Avatar, Join Date: Mar 2007
Newbie Member
Hi,

I just want to copy the partial array value of the pointer array
e.g
a start index 0-4 and argv index is from 5 to 9
a[0]=argv[5];
gohgss's Avatar, Join Date: Mar 2007
Newbie Member
In the other word, just to duplicate another subset of char array pointer.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
argv is not a one dimension array but its array of pointers and so you should be copying them to the other array of pointers and not into just an array. As a pseudo code
Code:
for(int i = 0; i<255;i++)
 a[i] = arg[1][i]; // Copy the content of the second array
ganesh_don's Avatar
Go4Expert Member
Mr.shabbir i think we cant do so bcoz a[i] is an array of pointers(as declared by gohgss) and what arg[1][i] points to is a character we cant store a charater into a pointer variable.Sorry if iam wrong
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You can copy argv [5]-argv [9] to a [0]-a[4], which will give you a duplicate set of pointers to the material, or you can use, say, strdup, passing argv [5]-argv [9] and store the returned pointers in a[0]-a[4], which will give you a duplicate set of strings. In the latter case, don't forget to free the contents of a[] when finished.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by ganesh_don
Mr.shabbir i think we cant do so bcoz a[i] is an array of pointers(as declared by gohgss) and what arg[1][i] points to is a character we cant store a charater into a pointer variable.Sorry if iam wrong
As a pseudo code I did not aim at giving the complete code but an idea. What I meant there is loop through it to copy it.