How to copy char array to char array?

Discussion in 'C' started by gohgss, Mar 14, 2007.

  1. gohgss

    gohgss New Member

    Joined:
    Mar 14, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    int main(int arg, char *argv [])
    {

    char *a[256];

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


    return 0;
    }
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    "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.
     
  3. gohgss

    gohgss New Member

    Joined:
    Mar 14, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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];
     
  4. gohgss

    gohgss New Member

    Joined:
    Mar 14, 2007
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    In the other word, just to duplicate another subset of char array pointer.
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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
     
  6. ganesh_don

    ganesh_don New Member

    Joined:
    Mar 9, 2007
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Mr.shabbir i think we cant do so bcoz a is an array of pointers(as declared by gohgss) and what arg[1] points to is a character we cant store a charater into a pointer variable.Sorry if iam wrong
     
  7. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    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.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice