琴长不过时光 发表于 2020-3-11 19:41:13

小白求助 结构体指针的问题

#define _CRT_SECURE_NO_WARNINGS
#define NUM 3
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct Computer
{
        char brand;
        double price;
        char color;
}TEAC;

void printComputer(TEAC* t,int i)
{
        printf("电脑的品牌为:%s\n", t.brand);
        printf("电脑的价格为:%f\n", t.price);
        printf("电脑的颜色为:%s\n", t.color);
}

void getMaxPrice(TEAC* t)//问题:这里形参为啥要用一级指针,用元素TEAC t不行吗?
{
        if (t.price > t.price)
        {
                if (t.price > t.price)
                {
                        printComputer(t, 0);
                }
                else//t.price < t.price
                {
                        printComputer(t, 2);
                }
        }
        else//t.price < t.price
        {
                if (t.price > t.price)
                {
                        printComputer(t, 1);
                }
                else //t.price < t.price
                {
                        printComputer(t, 2);
                }
        }

}

void main()
{
        int i;
        TEAC t1 = { "华为",2000.5,"红色" };
        TEAC t2, t3;
        strcpy(t2.brand, "联想");
        t2.price = 5000.0;
        strcpy(t2.color, "紫色");

        strcpy(t3.brand, "西门子");
        t3.price = 4000.5;
        strcpy(t3.color, "蓝色");

        TEAC t = { t1,t2,t3 };
        for (i = 0; i < NUM; ++i)
        {
                printComputer(&t, i);
        }

        printf("==============\n");

        getMaxPrice(&t);
        system("pause");
}

琴长不过时光 发表于 2020-3-11 19:43:58

#define _CRT_SECURE_NO_WARNINGS
#define NUM 3
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct Computer
{
        char brand;
        double price;
        char color;
}TEAC;

void printComputer(TEAC* t,int i)
{
        printf("电脑的品牌为:%s\n", t.brand);
        printf("电脑的价格为:%f\n", t.price);
        printf("电脑的颜色为:%s\n", t.color);
}

void getMaxPrice(TEAC* t)
{
        if (t.price > t.price)
        {
                if (t.price > t.price)
                {
                        printComputer(t, 0);
                }
                else//t.price < t.price
                {
                        printComputer(t, 2);
                }
        }
        else//t.price < t.price
        {
                if (t.price > t.price)
                {
                        printComputer(t, 1);
                }
                else //t.price < t.price
                {
                        printComputer(t, 2);
                }
        }

}

void main()
{
        int i;
        TEAC t1 = { "华为",2000.5,"红色" };
        TEAC t2, t3;
        strcpy(t2.brand, "联想");
        t2.price = 5000.0;
        strcpy(t2.color, "紫色");

        strcpy(t3.brand, "西门子");
        t3.price = 4000.5;
        strcpy(t3.color, "蓝色");

        TEAC t = { t1,t2,t3 };
        for (i = 0; i < NUM; ++i)
        {
                printComputer(t, i);
        }

        printf("==============\n");

        getMaxPrice(t);
        system("pause");
}
上面代码出了点错 下面贴新的代码 是因为数组名t是指针 所以才用一级指针来接的吗?
如果不修改指针的内容,比如打印数据 直接用一级指针?
如果修改指针的指向 就用二级指针?

4goodworld 发表于 2020-3-11 22:46:41

void getMaxPrice(TEAC* t)//问题:这里形参为啥要用一级指针,用元素TEAC t不行吗?
结合的例子,你既然要比较,也就是说所谓的t应该不是一个,那请问,如果你就传 TEAC t ,你如何表示多个?
你不想用指针,我们就直接传数组,那参数是不是也得 TEAC t[]?
我们延展下,arr[]数组与指针*arr 在实际使用中的差别大吗?arr几乎等同于*p,那你用指针有和不可呢?

至于你后面的观点
如果不修改指针的内容,比如打印数据 直接用一级指针?
如果修改指针的指向 就用二级指针?
我觉得可以这么理解
页: [1]
查看完整版本: 小白求助 结构体指针的问题