Home > database >  printCat1(Cat* cat) Vs. printCat2(Cat cat) & More Pointers' Questions
printCat1(Cat* cat) Vs. printCat2(Cat cat) & More Pointers' Questions

Time:12-11

The following questions might have been asked already. In addition, I am aware of the fact that there are a lot of posts that discuss the topic. However, after searching, I couldn't find answers to those specific questions.

Note: the questions appear below the code.

The code:

#include <stdio.h>

#define ARRAY_SIZE 3

typedef struct
{
    const char* name;
    const char* color;
    int age;
}Cat;

void printCat1(Cat* cat)
{
    printf("\n%s\n", cat->name);
    printf("%s\n", cat->color);
    printf("%d\n", cat->age);
    printf("\n");
}

void printCat2(Cat cat)
{
    printf("\n%s\n", cat.name);
    printf("%s\n", cat.color);
    printf("%d\n", cat.age);
    printf("\n");
}

void printCatArray(Cat catArr[])
{
    int i = 0;
    for (i = 0; i < ARRAY_SIZE; i  )
    {
        //WHICH OPTION IS BETTER? (printCat1 OR printCat2)
        //CALLING TO PRINTING FUNCTION.
    }

}

void swap(_____ cat1, _____ cat2)
{
    Cat temp = //cat1 or *cat1 ?
    //cat1 = cat2 or *cat1 = *cat2?
    cat2 = temp;
}

void sortbyage(Cat catarr[])
{
    int i, j;
    for (i = 0; i < ARRAY_SIZE - 1; i  )
        for (j = 1; j < ARRAY_SIZE; j  )
            if (catarr[i].age > catarr[j].age)
                swap(______, ______]);
}
int main() {
    Cat catArray[ARRAY_SIZE] =
    { {"cat1", "white", 1},
      {"cat2", "black", 2},
      {"cat3", "gray", 3} };


    printCatArray(catArray);


    return 0;
}

The questions:

1. What is the difference between both functions that print the data of a single cat structure?

2. Which printing function is better to use and why? it will be essential and meaningful if you would like to explain.

3. What is better to write and why? void swap(Cat cat1, Cat cat2) OR void swap(Cat* cat1, Cat* cat2)

4. Is the calling to swap function from the function soryByAge, swap(&catArr[i], &catArr[j]), correct? Would you write it differently?

5. The following line of code is correct: catArray[2] = catArray[1]; It will be great to get an explanation about what it actually does.

If one or more of the questions are not clear enough, I will be glad to clarify them.

Thank you very much beforehand!

CodePudding user response:

printCat1 uses a pointer, it's more efficient because it doesn't make a temporary duplicate.

printCat2 passes the value, it makes a duplicate of cat, it's less efficient.

swap function has to use pointers. If it doesn't, it will simply swap copied values. The original array will be unchanged.

void swap(Cat *cat1, Cat* cat2)
{
    Cat temp = *cat1;
    *cat1 = *cat2;
    *cat2 = temp;
}

When passing parameter to swap, use the address of the variable. For example,

swap(&arr[i], &arr[j]);
printCat1(&arr[i]);

The structure:

typedef struct
{
    const char* name;
    const char* color;
    int age;
}Cat;

...
Cat catArray[ARRAY_SIZE] =
{ {"cat1", "white", 1},
  {"cat2", "black", 2},
  {"cat3", "gray", 3} };

You want to declare strings as character arrays.

typedef struct
{ char name[50]; char color[50]; int age; }Cat;

Or declare as pointers and allocated dynamically.

CodePudding user response:

  1. I would not declare cat name and colour as const. If they are defined as const you will not be able to change it runtime, for example to get it from the user or read the file with your database.

  2. foo(cat cats[]) and foo(cat *cats) are exactly the same as arrays are passed as pointers. It is good to pass the size too as in real life you do not know big the array is.

  3. void swap(Cat cat1, Cat cat2) OR void swap(Cat* cat1, Cat* cat2) - the fist one will work on local copies of the structures. It will not affect the original array. So only the second version will do something meangfull.

  4. It is the correct way of doing it. You can also swap structs from the different arrays. You can also add index to the pointer (as in the example below)

  5. It copies the whole structure from the index 1 to the structure at index 2. After that you will have identical elements at indexes 1 and 2

typedef struct
{
    char* name;
    char* color;
    int age;
}Cat;

void printCat1(const Cat* cat)
{
    printf("\n%s\n", cat->name);
    printf("%s\n", cat->color);
    printf("%d\n", cat->age);
    printf("\n");
}

void printCat2(Cat cat)
{
    printf("\n%s\n", cat.name);
    printf("%s\n", cat.color);
    printf("%d\n", cat.age);
    printf("\n");
}

void printCatArray(const Cat *catArr, size_t size)
{
    for (size_t i = 0; i < size; i  )
    {
        printCat1(catArr   i);
        /* or */
        printCat2(catArr[i]);  // in this case the whole structure will passed to the function
    }

}

void swap(Cat *cat1, Cat *cat2)
{
    Cat temp = *cat1;
    *cat1 = *cat2;
    *cat2 = temp;
}

void sortbyage(Cat *catarr, size_t size)
{
    size_t i, j;
    for (i = 0; i < size - 1; i  )
        for (j = 1; j < size; j  )
            if (catarr[i].age > catarr[j].age)
                swap(catarr   i, catarr   j);
}
int main() {
    Cat catArray[] =
    { {"cat1", "white", 1},
      {"cat2", "black", 2},
      {"cat3", "gray", 3} };


    printCatArray(catArray, sizeof(catArray) / sizeof(catArray[0]));
}

https://godbolt.org/z/xodsToqxa

  • Related