How do I print the char array held in the struct variable char binary_filename
?
如何打印struct variable char binary_filename中保存的char数组?
I tried:
printf("Binary file name is : %s \n", prv_instance_t.binary_filename);
However, I get the error error: expected expression before ‘prv_instance_t’
但是,我收到错误错误:'prv_instance_t'之前的预期表达式
Here is the struct definition.
这是结构定义。
#define BINARY_FILE_NAME_MAXLEN 10
typedef struct _prv_instance_
{
/*
* The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
* is the instance scope user data (uint8_t power in this case)
*/
struct _prv_instance_ * next; // matches lwm2m_list_t::next
uint16_t shortID; // matches lwm2m_list_t::id
uint8_t power;
uint8_t reset;
double dec;
char binary_filename[BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;
2 个解决方案
#1
2
You are using the type itself. To access a member of the struct you have to declare an instance of that struct first. For example, this will print Hello World
:
您正在使用该类型本身。要访问结构的成员,您必须首先声明该结构的实例。例如,这将打印Hello World:
#include <stdio.h>
#include <string.h>
#define BINARY_FILE_NAME_MAXLEN 10
typedef struct _prv_instance_
{
char binary_filename [BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;
int main()
{
prv_instance_t foo, bar;
strcpy(foo.binary_filename, "Hello");
strcpy(bar.binary_filename, "World");
printf("%s %s\n", foo.binary_filename, bar.binary_filename);
return 0;
}
What you are trying to do is similar to
你想要做的是类似的
printf("%d", int);
#2
0
Just remove typedef
keyword.
只需删除typedef关键字即可。
Current definition does not define a variable, but makes two equivalent types prv_instance_t
and struct _prv_instance_
. From your question I understand that prv_instance_t
should be an instance (variable) of type _prv_instance_
, keyword typedef
is unnecessary.
当前定义不定义变量,而是生成两个等效类型prv_instance_t和struct _prv_instance_。根据您的问题,我了解prv_instance_t应该是_prv_instance_类型的实例(变量),关键字typedef是不必要的。
Your printf
will work with:
您的printf将用于:
#define BINARY_FILE_NAME_MAXLEN 10
struct _prv_instance_
{
/*
* The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
* is the instance scope user data (uint8_t power in this case)
*/
struct _prv_instance_ * next; // matches lwm2m_list_t::next
uint16_t shortID; // matches lwm2m_list_t::id
uint8_t power;
uint8_t reset;
double dec;
char binary_filename[BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;
as well as with the following one:
以及以下一个:
#define BINARY_FILE_NAME_MAXLEN 10
struct _prv_instance_
{
/*
* The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
* is the instance scope user data (uint8_t power in this case)
*/
struct _prv_instance_ * next; // matches lwm2m_list_t::next
uint16_t shortID; // matches lwm2m_list_t::id
uint8_t power;
uint8_t reset;
double dec;
char binary_filename[BINARY_FILE_NAME_MAXLEN];
};
struct _prv_instance_ prv_instance_t;
#1
2
You are using the type itself. To access a member of the struct you have to declare an instance of that struct first. For example, this will print Hello World
:
您正在使用该类型本身。要访问结构的成员,您必须首先声明该结构的实例。例如,这将打印Hello World:
#include <stdio.h>
#include <string.h>
#define BINARY_FILE_NAME_MAXLEN 10
typedef struct _prv_instance_
{
char binary_filename [BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;
int main()
{
prv_instance_t foo, bar;
strcpy(foo.binary_filename, "Hello");
strcpy(bar.binary_filename, "World");
printf("%s %s\n", foo.binary_filename, bar.binary_filename);
return 0;
}
What you are trying to do is similar to
你想要做的是类似的
printf("%d", int);
#2
0
Just remove typedef
keyword.
只需删除typedef关键字即可。
Current definition does not define a variable, but makes two equivalent types prv_instance_t
and struct _prv_instance_
. From your question I understand that prv_instance_t
should be an instance (variable) of type _prv_instance_
, keyword typedef
is unnecessary.
当前定义不定义变量,而是生成两个等效类型prv_instance_t和struct _prv_instance_。根据您的问题,我了解prv_instance_t应该是_prv_instance_类型的实例(变量),关键字typedef是不必要的。
Your printf
will work with:
您的printf将用于:
#define BINARY_FILE_NAME_MAXLEN 10
struct _prv_instance_
{
/*
* The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
* is the instance scope user data (uint8_t power in this case)
*/
struct _prv_instance_ * next; // matches lwm2m_list_t::next
uint16_t shortID; // matches lwm2m_list_t::id
uint8_t power;
uint8_t reset;
double dec;
char binary_filename[BINARY_FILE_NAME_MAXLEN];
} prv_instance_t;
as well as with the following one:
以及以下一个:
#define BINARY_FILE_NAME_MAXLEN 10
struct _prv_instance_
{
/*
* The first two are mandatories and represent the pointer to the next instance and the ID of this one. The rest
* is the instance scope user data (uint8_t power in this case)
*/
struct _prv_instance_ * next; // matches lwm2m_list_t::next
uint16_t shortID; // matches lwm2m_list_t::id
uint8_t power;
uint8_t reset;
double dec;
char binary_filename[BINARY_FILE_NAME_MAXLEN];
};
struct _prv_instance_ prv_instance_t;