表达式必须具有指向struct或union错误的指针 [英] Expression must have pointer to struct or union error

查看:675
本文介绍了表达式必须具有指向struct或union错误的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

t变量从assigntime函数开始出现错误,表示它必须具有指向结构或联合类型的指针.指针是我的弱点,如果有人可以解释,不仅给我答案,我需要做些什么来解决这个问题,这将是最有帮助的!欢呼.

//MY TIME C FILE
#include "my_time.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct my_time_int 
{
    int hour;
    int minute;
    int second;
};

void init_my_time(my_time *t)
{

    t=(my_time)malloc(sizeof(struct init_my_time*)); 
}

/*
 * Alter hour, minute, and second
 * Param h new value for hour
 * Param m new value for minute
 * Param s new value for second
*/
void assignTime(my_time *t, int h, int m, int s) 
{
    t->hour = h;
    t->minute = m;
    t->second = s;
}
//FOLLOWING CODE T VARIABLE HAS RED UNDERLINE ERROR SAYING EXPRESSION MUST HAVE POINTER TO STRUCT OR UNION>
char *toString(my_time t)
{ 
    char *r = (char *)malloc(12 * sizeof(char));

    if (t.hour >= 12) {
    if (t.hour == 12)
        sprintf(r, "%02d:%02d:%02d PM", 12, t.minute, t.second);
    else
        sprintf(r, "%02d:%02d:%02d PM", t.hour - 12, t.minute, t.second);
    }
    else {
        if (t.hour == 0)
        sprintf(r, "%02d:%02d:%02d AM", 12, t.minute, t.second);
        else
        sprintf(r, "%02d:%02d:%02d AM", t.hour, t.minute, t.second);
    }

    return r;

}

/*
 * Find printable form of time in 24 hour mode
 * Return String form of time in 24 hour mode for printing etc.
*/
char *toMilString(my_time t)
{ 
    char *s = (char *)malloc(9 * sizeof(char));

    sprintf(s, "%02d:%02d:%02d", t.hour, t.minute, t.second);
    return s;
}

/*
 * Find number of seconds elapsed since midnight
 * Return number of seconds elapsed since midnight as int
*/
int secsSinceMidnight(my_time t)
{
return t.second + (60 * t.minute) + (60 * 60 * t.hour);
}

此处的头文件:

#include <stdbool.h>

struct my_time_int;
typedef struct my_time_int *my_time;

void init_my_time(my_time *t);
void assignTime(my_time *t, int h, int m, int s);
void addTime(my_time t, double s);
char *toString(my_time t);
char *toMilString(my_time t);
bool equals(my_time this, my_time that);
bool my_timeIncHour(my_time *t);
bool my_timeIncMinute(my_time *t);
bool my_timeIncSecond(my_time *t);

解决方案

您的代码中有几个错误.

主要使用指针,对于预期结果而言,它是不正确的.在标题中,您需要输入以下行:

typedef struct my_time_int *my_time;

可以有效地声明my_time是指向struct my_time_int的指针类型.但是在函数的原型(以及定义)中,您使用指向my_time的指针作为参数:my_time* t.实际上,您在这里使用的是指向结构my_time_int的指针.

因此,当您尝试使用引用箭头运算符->分配给t时,会出错,因为实际上您是在为指向结构的指针分配指针,而不是为结构的普通指针分配指针.你想要的.

您还应该避免在my_time类型的变量上使用.运算符,因为它们实际上是指针.您应该在其上使用箭头->运算符.

以下是建议的解决方案:

//MY TIME C FILE
#include "prova.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct my_time_int
{
    int hour;
    int minute;
    int second;
};

//void init_my_time(my_time *t)
my_time init_my_time()
{
    //t=(my_time)malloc(sizeof(struct init_my_time*));
    return (my_time)malloc(sizeof(struct my_time_int));
}

/*
 * Alter hour, minute, and second
 * Param h new value for hour
 * Param m new value for minute
 * Param s new value for second
*/
//void assignTime(my_time *t, int h, int m, int s) 
void assignTime(my_time t, int h, int m, int s)
{
    t->hour = h;
    t->minute = m;
    t->second = s;
} 

//FOLLOWING CODE T VARIABLE HAS RED UNDERLINE ERROR SAYING EXPRESSION MUST             HAVE POINTER TO STRUCT OR UNION>
char *toString(my_time t)
{ 
char *r = (char *)malloc(12 * sizeof(char));

//if (t.hour >= 12) {
if(t->hour >= 12){
//if (t.hour == 12)
if(t->hour == 12)
    //sprintf(r, "%02d:%02d:%02d PM", 12, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d PM", 12, t->minute, t->second);
else
    //sprintf(r, "%02d:%02d:%02d PM", t.hour - 12, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d PM", t->hour - 12, t->minute, t->second);
}
else {
    //if (t.hour == 0)
    if (t->hour == 0)
    //sprintf(r, "%02d:%02d:%02d AM", 12, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d AM", 12, t->minute, t->second);
    else
    //sprintf(r, "%02d:%02d:%02d AM", t.hour, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d AM", t->hour, t->minute, t->second);
}

return r;

}

/*
 * Find printable form of time in 24 hour mode
 * Return String form of time in 24 hour mode for printing etc.
*/
char *toMilString(my_time t)
{ 
    char *s = (char *)malloc(9 * sizeof(char));

    //sprintf(s, "%02d:%02d:%02d", t.hour, t.minute, t.second);    
    sprintf(s, "%02d:%02d:%02d", t->hour, t->minute, t->second);
    return s;
}

/*
 * Find number of seconds elapsed since midnight
 * Return number of seconds elapsed since midnight as int
*/
int secsSinceMidnight(my_time t)
{
//return t.second + (60 * t.minute) + (60 * 60 * t.hour);
return t->second + (60 * t->minute) + (60 * 60 * t->hour);
}

还有标题:

#include <stdbool.h>

struct my_time_int;
typedef struct my_time_int *my_time;

//void init_my_time(my_time *t);
my_time init_my_time();
//void assignTime(my_time *t, int h, int m, int s);
void assignTime(my_time t, int h, int m, int s);

//and son on removing the unnecessary pointer types
void addTime(my_time t, double s);
char *toString(my_time t);
char *toMilString(my_time t);
bool equals(my_time this, my_time that);
bool my_timeIncHour(my_time t);
bool my_timeIncMinute(my_time t);
bool my_timeIncSecond(my_time t);

您可以在注释的代码中找到以前的错误声明和定义.

编辑

正如注释中指出的,init_my_time定义为泄漏内存,因为它分配了一个指针,该指针不会返回给调用者.正确的做法是分配内存,并将指向该内存的指针返回给调用方.这需要更改init_my_time的声明和定义,如上面在代码中所做的那样.

t variable is coming up with an error from assigntime function onwards saying it must have a pointer to a struct or union type. pointers are my weakness, if anyone could explain, not just give me the answer, what i need to do to fix this that would be most helpful! cheers.

//MY TIME C FILE
#include "my_time.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct my_time_int 
{
    int hour;
    int minute;
    int second;
};

void init_my_time(my_time *t)
{

    t=(my_time)malloc(sizeof(struct init_my_time*)); 
}

/*
 * Alter hour, minute, and second
 * Param h new value for hour
 * Param m new value for minute
 * Param s new value for second
*/
void assignTime(my_time *t, int h, int m, int s) 
{
    t->hour = h;
    t->minute = m;
    t->second = s;
}
//FOLLOWING CODE T VARIABLE HAS RED UNDERLINE ERROR SAYING EXPRESSION MUST HAVE POINTER TO STRUCT OR UNION>
char *toString(my_time t)
{ 
    char *r = (char *)malloc(12 * sizeof(char));

    if (t.hour >= 12) {
    if (t.hour == 12)
        sprintf(r, "%02d:%02d:%02d PM", 12, t.minute, t.second);
    else
        sprintf(r, "%02d:%02d:%02d PM", t.hour - 12, t.minute, t.second);
    }
    else {
        if (t.hour == 0)
        sprintf(r, "%02d:%02d:%02d AM", 12, t.minute, t.second);
        else
        sprintf(r, "%02d:%02d:%02d AM", t.hour, t.minute, t.second);
    }

    return r;

}

/*
 * Find printable form of time in 24 hour mode
 * Return String form of time in 24 hour mode for printing etc.
*/
char *toMilString(my_time t)
{ 
    char *s = (char *)malloc(9 * sizeof(char));

    sprintf(s, "%02d:%02d:%02d", t.hour, t.minute, t.second);
    return s;
}

/*
 * Find number of seconds elapsed since midnight
 * Return number of seconds elapsed since midnight as int
*/
int secsSinceMidnight(my_time t)
{
return t.second + (60 * t.minute) + (60 * 60 * t.hour);
}

Header File Here:

#include <stdbool.h>

struct my_time_int;
typedef struct my_time_int *my_time;

void init_my_time(my_time *t);
void assignTime(my_time *t, int h, int m, int s);
void addTime(my_time t, double s);
char *toString(my_time t);
char *toMilString(my_time t);
bool equals(my_time this, my_time that);
bool my_timeIncHour(my_time *t);
bool my_timeIncMinute(my_time *t);
bool my_timeIncSecond(my_time *t);

解决方案

There are a couple of errors in your code.

Primarily the use of pointers it's not correct in respect to the desired outcome. In the header you have the line:

typedef struct my_time_int *my_time;

which effectively declares my_timebeing a pointer type to the struct my_time_int. But in your function's prototypes (and definitions too) you use a pointer to my_time as argument: my_time* t. In fact here you are using a pointer to a pointer to the struct my_time_int.

So when you try to assign to tusing the deference arrow operator -> you make a mistake, because in fact you are assigning to a pointer to a pointer to a struct not to a plain pointer to a struct as you wish.

You should also avoid using the . operator on variables of type my_time because they are in facts pointers. You should use instead the arrow ->operator on them.

Here the proposed solution:

//MY TIME C FILE
#include "prova.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

struct my_time_int
{
    int hour;
    int minute;
    int second;
};

//void init_my_time(my_time *t)
my_time init_my_time()
{
    //t=(my_time)malloc(sizeof(struct init_my_time*));
    return (my_time)malloc(sizeof(struct my_time_int));
}

/*
 * Alter hour, minute, and second
 * Param h new value for hour
 * Param m new value for minute
 * Param s new value for second
*/
//void assignTime(my_time *t, int h, int m, int s) 
void assignTime(my_time t, int h, int m, int s)
{
    t->hour = h;
    t->minute = m;
    t->second = s;
} 

//FOLLOWING CODE T VARIABLE HAS RED UNDERLINE ERROR SAYING EXPRESSION MUST             HAVE POINTER TO STRUCT OR UNION>
char *toString(my_time t)
{ 
char *r = (char *)malloc(12 * sizeof(char));

//if (t.hour >= 12) {
if(t->hour >= 12){
//if (t.hour == 12)
if(t->hour == 12)
    //sprintf(r, "%02d:%02d:%02d PM", 12, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d PM", 12, t->minute, t->second);
else
    //sprintf(r, "%02d:%02d:%02d PM", t.hour - 12, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d PM", t->hour - 12, t->minute, t->second);
}
else {
    //if (t.hour == 0)
    if (t->hour == 0)
    //sprintf(r, "%02d:%02d:%02d AM", 12, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d AM", 12, t->minute, t->second);
    else
    //sprintf(r, "%02d:%02d:%02d AM", t.hour, t.minute, t.second);
    sprintf(r, "%02d:%02d:%02d AM", t->hour, t->minute, t->second);
}

return r;

}

/*
 * Find printable form of time in 24 hour mode
 * Return String form of time in 24 hour mode for printing etc.
*/
char *toMilString(my_time t)
{ 
    char *s = (char *)malloc(9 * sizeof(char));

    //sprintf(s, "%02d:%02d:%02d", t.hour, t.minute, t.second);    
    sprintf(s, "%02d:%02d:%02d", t->hour, t->minute, t->second);
    return s;
}

/*
 * Find number of seconds elapsed since midnight
 * Return number of seconds elapsed since midnight as int
*/
int secsSinceMidnight(my_time t)
{
//return t.second + (60 * t.minute) + (60 * 60 * t.hour);
return t->second + (60 * t->minute) + (60 * 60 * t->hour);
}

And header too:

#include <stdbool.h>

struct my_time_int;
typedef struct my_time_int *my_time;

//void init_my_time(my_time *t);
my_time init_my_time();
//void assignTime(my_time *t, int h, int m, int s);
void assignTime(my_time t, int h, int m, int s);

//and son on removing the unnecessary pointer types
void addTime(my_time t, double s);
char *toString(my_time t);
char *toMilString(my_time t);
bool equals(my_time this, my_time that);
bool my_timeIncHour(my_time t);
bool my_timeIncMinute(my_time t);
bool my_timeIncSecond(my_time t);

As you can se in the commented code there are the previous erroneous declarations and definitions.

EDIT

As pointed in the comments, the init_my_time, as defined, leaks memory because allocates a pointer that it doesn't return to the caller. The right thing to do here is to allocate the memory and return the pointer to that memory to the caller. This requires changing the declaration and definition of init_my_time as already done above in the code.

这篇关于表达式必须具有指向struct或union错误的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆