4179 views|7 replies

2865

Posts

4

Resources
The OP
 

How to define a const structure variable [Copy link]

I have defined a structure and I want all definitions to be placed in the code area instead of RAM memory.

typedef struct 
{
   const int id;
   const char *name;
   struct NodeType *Link;
}NodeType;


const char* str1="tom";

NodeType node1,node2,node3;

main{
        node1.id = 1;
		node1.name = str1;
}

All definitions are fine, but it fails when initializing node1.id = 1; The compiler used is keil 5

This post is from Embedded System

Latest reply

const needs to be initialized   Details Published on 2021-7-9 21:45

6040

Posts

204

Resources
2
 

const NodeType node1={0, "tom", &node2};

It should be like this, and it should be initialized when declared. Unused members do not need to be specified as const.

Since you want to put it in the constant area, it is impossible to initialize it in the main function

This post is from Embedded System

Comments

It seems that pointers cannot be used. #144: a value of type "NodeType *" cannot be used to initialize an entity of type "const struct NodeType *"  Details Published on 2021-7-9 14:52
 
 

1942

Posts

2

Resources
3
 

If you have a const type in your structure, you need to initialize it when you create it, just like the above.

This post is from Embedded System
 
 
 

2865

Posts

4

Resources
4
 

const typedef struct 
{
   const int  id;
	 const char *name;
	 const struct NodeType *Link;
}NodeType;

/*=====================================*/


const  NodeType node3={2,"",NULL};
const  NodeType	node2={1,"jack",&node3};
const  NodeType node1={0,"tom",&node2};

Thank you very much. I just tried it and only const NodeType node3={2,"",NULL}; works. The others cannot be compiled.

This post is from Embedded System
 
 
 

2865

Posts

4

Resources
5
 

I thought it was a problem with the symbol &node3, but it seems not.

It seems that pointers cannot be used in structures???

This post is from Embedded System
 
 
 

2865

Posts

4

Resources
6
 
lcofjp posted on 2021-7-9 12:39 const NodeType node1={0, "tom", &node2}; It should be like this, and it should be initialized when declared. There is no need inside...

It seems that pointers cannot be used.

#144: a value of type "NodeType *" cannot be used to initialize an entity of type "const struct NodeType *"

This post is from Embedded System
 
 
 

6040

Posts

204

Resources
7
 

Because the syntax of the structure you defined is incorrect, I didn't mention it above so I thought it was a typo in your post.

typedef struct _NodeType
{
   const int id;
   const char *name;
   struct _NodeType *Link;
}NodeType;

This post is from Embedded System

赞赏

1

查看全部赞赏

 
 
 

7422

Posts

2

Resources
8
 

const needs to be initialized

This post is from Embedded System
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list