我有一个带有指针的方法,我想根据输入将它放在另一个指针中。
这是一个过度简化的版本:
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) 我有一个结构 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)