Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Delaration for An Array in C?

Status
Not open for further replies.

eepty

Full Member level 2
Joined
Oct 21, 2005
Messages
143
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,611
I am writting a firmware in C for a home appliance. In a C file for the real time clock, the complier reported error with an error message: "constant expression expected in initializer" in line 8 (the declaration of the array):

//In the c file:
1 char now_sec;
2 char now_min;
3 char now_hr;
4 char now_day;
5 char now_weekday;
6 char now_month;
7 char now_year;
8 char now_clock[]={now_sec, now_min, now_hr, now_day, now_weekday, now_month, now_year};

//in the h file:
extern char now_sec; //current second
extern char now_min; //current minute
extern char now_hr; //current hour
extern char now_day; //current day
extern char now_weekday; //current weekday
extern char now_month; //current month
extern char now_year; //current year
extern char now_clock[]={now_sec, now_min, now_hr, now_day, now_weekday, now_month, now_year};

What is wrong here?:?:
Thanks in advance! :grin:
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
I think the compiler is actually complaining that you’re trying to write C by trial and error and would probably suggest you read a book about the basics of C, but it is too polite...

What you wrote in line 8 has no meaning in C. You can only initialize variables (your array in this case) with constants whose values are known at compile time. You are using variable names instead and that makes no sense. Even if it worked you would actually allocate two times the storage needed for your variables.

I’m not sure what your intention was originally, but here are 2 ways of rewriting that:
Code:
char *now_clock[]={&now_sec, &now_min, &now_hr, &now_day, &now_weekday, &now_month, &now_year};
or:
Code:
union
{
	char init[7];	// use this to initialize the clock in a for loop
	struct
	{
		char now_sec;
		char now_min;
		char now_hr;
		char now_day;
		char now_weekday;
		char now_month;
		char now_year;
	};
}now_clock;

Your homework is to find out what these cases mean and how they apply to your need.

Arthur
 
  • Like
Reactions: eepty

    eepty

    Points: 2
    Helpful Answer Positive Rating

eepty

Full Member level 2
Joined
Oct 21, 2005
Messages
143
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,611
I think the compiler is actually complaining that you’re trying to write C by trial and error and would probably suggest you read a book about the basics of C, but it is too polite...

What you wrote in line 8 has no meaning in C. You can only initialize variables (your array in this case) with constants whose values are known at compile time. You are using variable names instead and that makes no sense. Even if it worked you would actually allocate two times the storage needed for your variables.

I’m not sure what your intention was originally, but here are 2 ways of rewriting that:
Code:
char *now_clock[]={&now_sec, &now_min, &now_hr, &now_day, &now_weekday, &now_month, &now_year};
or:
Code:
union
{
	char init[7];	// use this to initialize the clock in a for loop
	struct
	{
		char now_sec;
		char now_min;
		char now_hr;
		char now_day;
		char now_weekday;
		char now_month;
		char now_year;
	};
}now_clock;

Your homework is to find out what these cases mean and how they apply to your need.

Arthur

Thank you very much! Really!
In fact I spent some time to search the web for the answer and could not success.
I learnt C 6 years ago when I am still studing in University. Then I seldom touch C after I finished the undergraduate study. Now my boss ask me to write a C software, in one week. What would you do? I think sit down and try is better than studying a book in this situation, right?

Anyway thanks for your help
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
I'm glad I could help (you show it normally by clicking the “Helpful” button in the lower left of the post).

I didn’t want to sound condescending. Everyone was a beginner and everyone forgets, but, on the other hand, I do actually believe you will be productive much faster and will write better code if you refresh the basics and plan your work with a flowchart on a piece of paper, rather than coding by trial and error or adapting other’s code.
I can actually bet on it!

Arthur
 

keith1200rs

Super Moderator
Staff member
Joined
Oct 9, 2009
Messages
10,877
Helped
2,064
Reputation
4,128
Reaction score
1,597
Trophy points
1,403
Location
Yorkshire, UK
Activity points
57,276
Try the O'Reilly C Pocket Guide or something similar. I find small guides like that better for quickly refreshing my memory than digging out the 'big books'.

Keith
 
  • Like
Reactions: sout and eepty

    eepty

    Points: 2
    Helpful Answer Positive Rating

    sout

    Points: 2
    Helpful Answer Positive Rating

eepty

Full Member level 2
Joined
Oct 21, 2005
Messages
143
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,611
Try the O'Reilly C Pocket Guide or something similar. I find small guides like that better for quickly refreshing my memory than digging out the 'big books'.

Keith

That sound great! That is what I always want to get. Thanks
 

eepty

Full Member level 2
Joined
Oct 21, 2005
Messages
143
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,611
char *now_clock[]={&now_sec, &now_min, &now_hr, &now_day, &now_weekday, &now_month, &now_year};
Your first code is to declare an array of 7 pointers, and assign an address for each pointers, right?
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
Your first code is to declare an array of 7 pointers, and assign an address for each pointers, right?
Right!
I don't know what you require, but I would normally use the scond construct (the union) that allocates an array "on top" of the variables.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top