Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

copy an array in c

Name: Anonymous 2007-03-12 22:30 ID:016HOAcL

I have a function, it accepts a char**, what is the quickest and most efficient way of making a local copy of the array in the function scope?

That is, i have , for example:

void funk(char **x){

char **b;

/*How do i make a separate copy of x to b?*/
}

Take pity on me internets, I'm a noob :) Thanks.

Name: Anonymous 2007-03-15 18:11 ID:acmPTe2g

Answer to what?

Name: Anonymous 2007-03-16 7:15 ID:cZZ6uzeL

It's kind of pointless arguing about null-terminated strings in C. Want to use the standard libraries? You don't really have a choice _but_ to use them. Don't want null-terminated strings? Roll your own string API, and be prepared to not be able to pass your custom strings to the standard library system functions which expect them.

Name: Anonymous 2007-03-16 7:47 ID:gF+Kn3aW

>>41
See post >>42 for the question.

Name: Anonymous 2008-04-23 10:00

Copying arrays? Forget it, it's NP-Complete.

Name: Anonymous 2009-03-06 13:44

Pointer to the array is too short   to read SICP   and achieved Satori   PROGRAMMING IS ALL   YOU NEED OTHERWISE   YOU ARE A   few other alternatives   but none are   all that mature   yet From what   I gather so   what should I   call you Kris   reading from my   socialist state I   said a buggy!

Name: Anonymous 2010-12-17 1:29

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Sgt.Kabu聸၍kiman焾ᾔ 2012-05-28 23:12

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Name: Anonymous 2012-05-28 23:57


// assuming x is a null terminated array of null terminated strings
// untested
void funk(char **x) {
    // calculate length of x
    size_t len;
    for (len = 0; x[len]; ++len);
    // allocate copy
    char **copy = malloc((len + 1) * sizeof(*x));
    // copy over strings
    for (size_t i = 0; i < len; ++i) {
        copy[i] = malloc(strlen(x[i]));
        strcpy(copy[i], x[i]);
    }
    // add null terminator
    copy[len] = 0;
}

Name: Anonymous 2012-05-29 0:02

>>51

I forgot nulls on strings


// assuming x is a null terminated array of null terminated strings
// untested
void funk(char **x) {
    // calculate length of x
    size_t len;
    for (len = 0; x[len]; ++len);
    // allocate copy
    char **copy = malloc((len + 1) * sizeof(*x));
    // copy over strings
    for (size_t i = 0; i < len; ++i) {
        size_t _len = strlen(x[i]);
        copy[i] = malloc(_len + 1);
        strcpy(copy[i], x[i]);
        copy[_len] = 0;
    }
    // add null terminator
    copy[len] = 0;
}

Name: Anonymous 2012-05-29 0:03

>>52

oops copy[_len]...


// assuming x is a null terminated array of null terminated strings
// untested
void funk(char **x) {
    // calculate length of x
    size_t len;
    for (len = 0; x[len]; ++len);
    // allocate copy
    char **copy = malloc((len + 1) * sizeof(*x));
    // copy over strings
    for (size_t i = 0; i < len; ++i) {
        size_t _len = strlen(x[i]);
        copy[i] = malloc(_len + 1);
        strcpy(copy[i], x[i]);
        copy[i][_len] = 0;
    }
    // add null terminator
    copy[len] = 0;
}

Name: Anonymous 2012-05-29 0:05

>>53

oh strcpy copys over null terminated so i need to change this again.


// assuming x is a null terminated array of null terminated strings
// untested
void funk(char **x) {
    // calculate length of x
    size_t len;
    for (len = 0; x[len]; ++len);
    // allocate copy
    char **copy = malloc((len + 1) * sizeof(*x));
    // copy over strings
    for (size_t i = 0; i < len; ++i) {
        size_t _len = strlen(x[i]);
        copy[i] = malloc(_len + 1);
        strcpy(copy[i], x[i]);
    }
    // add null terminator
    copy[len] = 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List