小编yee*_*boi的帖子

C 如何将指针赋值给一个指针

我有一个带有指针的方法,我想根据输入将它放在另一个指针中。

这是一个过度简化的版本:

struct people
{
    char sName[20];
    int nAge;
};

void printDetails(struct people person)
{
    printf("Name: %s\nAge: %d\n", person.sName, person.nAge);
}

void changeAge(struct people *person1, struct people *person2, int nNewAge, int nPerson)
{
    /// I know that this is wrong. I don't know how to implement this. This is my best try.
    struct people *personToBeModified;
    if (nPerson == 1)
        *personToBeModified = *&(*person1);
    else if (nPerson == 2)
        *personToBeModified = *&(*person2);

    (*personToBeModified).nAge = nNewAge;
}
void main() 
{
    struct people person1 …
Run Code Online (Sandbox Code Playgroud)

c pointers

1
推荐指数
1
解决办法
72
查看次数

如何对结构类型的东西进行排序

我有一个结构 ElementType

typedef struct
{
    int AtomicNumber;
    char Name[31];
    char Symbol[4];
} ElementType;
Run Code Online (Sandbox Code Playgroud)

我正在尝试实现一种排序算法,该算法将按字母顺序对元素进行排序。我比较了字符串,但没有任何效果。我无法弄清楚我下面的功能有什么问题。

void sortAlphabetical(ElementType elements[NUM_ELEMENTS])
{
   printf("SORTING!\n");
   int c, d;
   for (c = 0 ; c < NUM_ELEMENTS - 1; c++)
   {
       for (d = 0 ; d < NUM_ELEMENTS - c - 1; d++)
       {
           if (elements[d].Name > elements[d+1].Name)
           {
                ElementType temp;

                temp.AtomicNumber = elements[d].AtomicNumber;
                strcpy(temp.Name, elements[d].Name);
                strcpy(temp.Symbol, elements[d].Symbol);

                elements[d].AtomicNumber = elements[d+1].AtomicNumber;
                strcpy(elements[d].Name, elements[d+1].Name);
                strcpy(elements[d].Symbol, elements[d+1].Symbol);

                elements[d+1].AtomicNumber = temp.AtomicNumber;
                strcpy(elements[d+1].Name, temp.Name);
                strcpy(elements[d+1].Symbol, temp.Symbol);
           }
       }
   }
}
Run Code Online (Sandbox Code Playgroud)

c sorting algorithm

0
推荐指数
1
解决办法
46
查看次数

标签 统计

c ×2

algorithm ×1

pointers ×1

sorting ×1