#include <stdio.h>
#include <string.h>
# # define MAX 50
typedef struct contact_st
{
char Name[10];
char PhoneNumber[13];
} } Contact;
Contact PhoneBook[MAX];
int contactSwap(void* arr, int i, int j)
{
int temp;
temp = ((int*)arr + i);
*((int*)arr + i) = ((int*)arr + j);
*((int*)arr + j) = temp;
// // Goal: arr[i] <- arr[j], arr[j] <- arr[i]
// // calculate address for arr[i]: ((int*)arr+i)
// // derefernce int: *((int*)arr+i))
}
int main() {
Contact A[5] = { { "Roki", "12345678" }, { "Thor", "33333333" }, { "IronMan","3765898" }, { "Hulk", "74561253" }, { "Groot", "99999999" } };
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4 - i; j++)
{
if (*((Contact*)A + i)->PhoneNumber, *((Contact*)A + j)->PhoneNumber){
contactSwap(A, i, j);
}
}
}
void *vp;
vp = A;
for (int i = 0; i < 5; i++)
{
// // Calculate address for arr[i]: ((Contact*)arr+i)
// // derefernce structure Contact: *((Contact*)arr+i)).Name
// // for stucture we use ->: ((Contact*)vp+i)->Name
printf("Addr vp:%p\t", (Contact*)vp + i);
printf("name:%s\t phone:%s\n", ((Contact*)vp + i)->Name, ((Contact*)vp + i)->PhoneNumber);
}
printf("\n");
}
The compilation works well, but I don't know if it's logical. I want to go down in the order of the names, but the order doesn't change and the first price gets weird. What should I do? Please I beg you.crying Is the function wrong? I wrote it in C.
pointer array swap
int contactSwap(Contact* arr, int i, int j);
Change it like this and try writing it again.
© 2024 OneMinuteCode. All rights reserved.