C 交换结构数组中的元素

C 交换结构数组中的元素,c,pointers,struct,C,Pointers,Struct,假设我有这个结构: struct MyStruct { int iID; int iMyNumber; }; 然后我定义了一个mystruct数组: struct MyStruct msTest[3]; 通过查看ID,我正在对一个类似于此的结构进行排序操作。现在,一旦我发现应该交换哪些记录来对数组进行排序,我就必须进行实际的交换。我试过这个: if (iSmallest != iCntr) { stPTmp = &stXDB[iCntr]; &stXD

假设我有这个结构:

struct MyStruct {
  int iID;
  int iMyNumber;
};
然后我定义了一个mystruct数组:

struct MyStruct msTest[3];
通过查看ID,我正在对一个类似于此的结构进行排序操作。现在,一旦我发现应该交换哪些记录来对数组进行排序,我就必须进行实际的交换。我试过这个:

if (iSmallest != iCntr) {
    stPTmp = &stXDB[iCntr];
    &stXDB[iCntr] = &stXDB[iSmallest];
    &stXDB[iSmallest] = &stPTmp;
}

stPTmp定义为
void*stPTmp
iCntr
ismalest
包含要交换的记录的索引。我的代码不工作,但如何修复它?

您需要交换元素,而不是指针

struct MyStruct stTmp;

if (iSmallest != iCntr) {
    stTmp = stXDB[iCntr];
    stXDB[iCntr] = stXDB[iSmallest];
    stXDB[iSmallest] = stTmp;
}

效率不太高,但您的结构很小,所以它只比交换指针贵一点。

您可以让其他人考虑一下,即使用

#包括
int compare_struct(const void*a,const void*b)
{
const struct MyStruct*sa=a,*sb=b;
返回(sa->iIDiID)?-1:sa->iID>sb->iID;
}
qsort(msTest,sizeof msTest/sizeof*msTest,sizeof*msTest,compare_struct);

注意,这完全消除了编写交换函数的需要。在引擎盖下,这可能会有点昂贵(可以使用
malloc()
,几乎可以肯定使用
memcpy()
),但它更易于编写和维护。

John已经回答了您的问题,但是要对
结构进行排序,您可以使用标准库
qsort()
函数:

#include <stdlib.h>
#include <stdio.h>

struct MyStruct {
    int iID;
    int iMyNumber;
};

/* comparison function, should return < 0, > 0 or == 0
   if a < b, a > b or a == b respectively.  Used by qsort */
static int comp_mystruct(const void *a, const void *b);

/* utility function to print an array of our struct */
static void print_mystruct(const void *start, size_t n);

int main(void)
{
    /* some data */
    struct MyStruct data[] = {
        { 1, 10 },
        { 5, 50 },
        { 2, 20 },
        { -3, 100 }
    };
    size_t ndata = sizeof data / sizeof data[0];

    /* before sorting */
    print_mystruct(data, ndata);
    putchar('\n');

    /* sort the array now */
    qsort(data, ndata, sizeof data[0], comp_mystruct);

    /* after sorting */
    print_mystruct(data, ndata);

    return 0;
}

static void print_mystruct(const void *start, size_t n)
{
    size_t i;
    const struct MyStruct *s = start;
    for (i=0; i < n; ++i) {
        printf("% 3d % 3d\n", s[i].iID, s[i].iMyNumber);
    }
}

static int comp_mystruct(const void *a, const void *b)
{
    const struct MyStruct *sa = a;
    const struct MyStruct *sb = b;
    if (sa->iID > sb->iID) {
        return 1;
    } else if (sa->iID < sb->iID) {
        return -1;
    } else {
        return 0;
    }
}
优点是
qsort()
是标准的,您可以使用它对任何内容进行排序

#include <stdlib.h>
#include <stdio.h>

struct MyStruct {
    int iID;
    int iMyNumber;
};

/* comparison function, should return < 0, > 0 or == 0
   if a < b, a > b or a == b respectively.  Used by qsort */
static int comp_mystruct(const void *a, const void *b);

/* utility function to print an array of our struct */
static void print_mystruct(const void *start, size_t n);

int main(void)
{
    /* some data */
    struct MyStruct data[] = {
        { 1, 10 },
        { 5, 50 },
        { 2, 20 },
        { -3, 100 }
    };
    size_t ndata = sizeof data / sizeof data[0];

    /* before sorting */
    print_mystruct(data, ndata);
    putchar('\n');

    /* sort the array now */
    qsort(data, ndata, sizeof data[0], comp_mystruct);

    /* after sorting */
    print_mystruct(data, ndata);

    return 0;
}

static void print_mystruct(const void *start, size_t n)
{
    size_t i;
    const struct MyStruct *s = start;
    for (i=0; i < n; ++i) {
        printf("% 3d % 3d\n", s[i].iID, s[i].iMyNumber);
    }
}

static int comp_mystruct(const void *a, const void *b)
{
    const struct MyStruct *sa = a;
    const struct MyStruct *sb = b;
    if (sa->iID > sb->iID) {
        return 1;
    } else if (sa->iID < sb->iID) {
        return -1;
    } else {
        return 0;
    }
}
  1  10
  5  50
  2  20
 -3  100

 -3  100
  1  10
  2  20
  5  50