2

I have defined struct like

typedef struct {
    char *oidkey;
    int showperf;
    char oidrealvalue[BUFSIZE];
    char *oidlimits;
} struct_oidpairs;

and I have array of struct

 struct_oidpairs b[] ={{.....},....}

and I want to copy it to new struct array a[]

please help

3
  • What's the point of typedef struct { ... } struct_oidpairs? Why not just struct oidpairs { ... }? Commented Sep 3, 2010 at 14:31
  • 1
    @JeremyP: These are not the same in C. Commented Sep 3, 2010 at 14:38
  • 2
    Yes they are. The difference is that in one case, you type a space (or any amount of white space) between the struct and the oidpairs. In the other case you type an underscore. Commented Sep 3, 2010 at 14:54

5 Answers 5

2

Something like this:

memcpy(dest, src, sizeof(struct) * sizeof(src));
Sign up to request clarification or add additional context in comments.

4 Comments

This will not work as expected if oidkey and oidlimits are dynamically allocated strings.
It makes a shallow copy of the struct, thus it copies the pointers, not the contents of the strings.
Yes as long as oidkey and oidlimits are constants and not subject to be changed in the copied version of the array AND both the source during the lifetime of the copied array
How does this memcpy differ from struct copying a[i]=b[i]; mentioned in the down voted answer (except for the b.size() part, of course)? If the array size is specified properly, wouldn't struct copying work similar to a shallow memcpy?
2

Your struct contains pointers as data members, this means you will have to roll out your own copy function that will do something sensible with the pointers. memcpy only works is all the data related to the struct is stored in the struct.

Comments

1

For real copy of the contents, follow Sjoerd's answer and then:

for (i = 0; i < sizeof(src); i++)
{
    if (src[i].oidkey != NULL)
    {
        dest[i].oidkey = malloc(strlen(src[i].oidkey) + 1);
        strcpy(dest[i].oidkey, src[i].oidkey);
    }

    if (src[i].oidlimits != NULL)
    {
        dest[i].oidlimits = malloc(strlen(src[i].oidlimits) + 1);
        strcpy(dest[i].oidlimits, src[i].oidlimits);
    }
}

You may consider memcpy if you are interested in speed.

Update:

  • Following harper's code, I updated the code to check NULL pointers
  • This is a quoted note from gordongekko:

    This solution will crash if oidkey or oidlimits are != NULL and not '\0'-terminated means not initialized

3 Comments

You must check for NULL pointers in oidkey and oidlimits to avoid undefined behavior (i.e. page fault). You may consider strdup instead of malloc+strcpy to reduce code lines. And you may use strdup instead of malloc+strcpy to save code lines.
your solution will crash if oidkey/oidlimits are !=NULL and not '\0'-terminated means not initialized.
@harper: strdup is not a standard function. In other words, there's no such function as strdup in C.
0

Makes NOT a deep copy:

struct_oidpairs b[] = {...};
size_t len = sizeof b/sizeof*b;
struct_oidpairs *a = malloc(sizeof b);

if( !a ) /* ERROR handling */

memcpy( a, b, sizeof b );
...
free( a );

or

while( len-- )
  a[len] = b[len];
...
free( a );

Comments

-5

maybe using :

for(int i=0; i<b.size() ;i++) 
//I am not sure if this is the correct name for the size function
{
a[i]=b[i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.