Continue to Site

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.

Problem after adding "code" to C structure

Status
Not open for further replies.

Help

Advanced Member level 2
Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Activity points
7,065
Hi,

Why after i add the "code" on structure then the code comeout the error? What can i do?

Code:
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

struct code{ uchar time; uint dly; }nxt[] = { {0,10}, {1,20}, {2,30}, {3,40}, {4,50}, {5,60}, {6,70} };
		   		
void main()
{
	uchar i;

	while(1)
	{
		for(i=0; i<7; i++)
		{
			P1 = nxt[i].time;
			P2 = nxt[i].dly;
 	    }
	}
}

Thank You.
 

Re: C Structure

You don't have declaration for variables P1 and P2. Without that it is hard to say what is the problem.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

Hi,

Let say we remove the P1 and P2. We replace that to x and y. We still get the same error! :|

Code:
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

struct code{ uchar time; uint dly; }nxt[] = { {0,10}, {1,20}, {2,30}, {3,40}, {4,50}, {5,60}, {6,70} };
		   		
void main()
{
	uchar i,x,y;

	while(1)
	{
		for(i=0; i<7; i++)
		{
			x = nxt[i].time;
			y = nxt[i].dly;
 	    }
	}
}
Thank You
 

C Structure

What does your error message say?

I tried compiling your second example in gcc, and it emitted two complaints, both easy to fix:

test.c:1:19: reg52.h: No such file or directory
test.c:9: warning: return type of 'main' is not `int'
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

When is this loop going to finish? You have endless loop with while(1), so your program never ends. Anyway, it will not give any output. I tried to compile it under gcc and there is not a problem with it. What is exactly your problem? Comiplation error or something else?

Added after 6 minutes:

Is code reserverd word on your platform?

Added after 1 minutes:

Is code reserved word on your platform?
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

Hi,

Sorry, I am using Keil compiler to simulate my code. You can download the compiler from..

**broken link removed**

After you download and install the compiler try and use the "C Structure" code that i prepare inside the zip file.

the compiler show the 4 error message:
1) syntax error near 'code'
2) syntax error near '{'
3) syntax error near '}'
4) left side of '.' requires struct/union

after ignore the code from the structure then there are no errors occur.

Thank you for your all helping....
 

Re: C Structure

It seams that code is reserved word in your compiler. Just rename it to soemthing else, and you should be fine.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

Hi,

cfant said:
It seams that code is reserved word in your compiler. Just rename it to soemthing else, and you should be fine.
What you mean? Do you got any ideas?

Thank You.
 

Re: C Structure

Code:
#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

struct code1{ uchar time; uint dly; }nxt[] = { {0,10}, {1,20}, {2,30}, {3,40}, {4,50}, {5,60}, {6,70} };
               
void main()
{
   uchar i,x,y;

   while(1)
   {
      for(i=0; i<7; i++)
      {
         x = nxt[i].time;
         y = nxt[i].dly;
        }
   }
}

Change your program. See above.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

p1,p2 are 8 bit ports ,dly is an integer variable dont we have to type cast it as char in order to get it on port pins.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
C Structure

dont apply type cast on p1, p2 and dly. declear them with corresponding type directly.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

Hi,

kingraja84 said:
p1,p2 are 8 bit ports ,dly is an integer variable dont we have to type cast it as char in order to get it on port pins.

Thanks for your remind! I already change dly to char variable type. But the error still there....

syntax error near 'code'
syntax error near '{'
syntax error near '}'
left side of '.' requires struct/union

Thank You.
 

Re: C Structure

Haven't you read my previous post. "code" is a reserved word, so you must use something else insted of it.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

Hi,

The "code" is one of the physical location of the memory spaces on the chip. So i prefer the time and dly variable is state on the code memory location.

How can i play around with my code? :|

Thank You.
 

Re: C Structure

If that is the case, do you know its address? You can use a pointer to the structure to access that location.

Added after 21 minutes:

Can you explain why your structure tag has to be named "code"? I tried to build your program under Keil, and if you change the program to:

#include <reg52.h>

#define uchar unsigned char
#define uint unsigned int

struct code1{ uchar time; uchar dly; }nxt[] = { {0,10}, {1,20}, {2,30}, {3,40}, {4,50}, {5,60}, {6,70} };

void main()
{
uchar i; uchar P1, P2;

while(1)
{
for(i=0; i<7; i++)
{
P1 = nxt.time;
P2 = nxt.dly;
}
}
}

everything is ok. You are using unnamed structure with tag code, but since this is a reserved word in Keil, the compilation fails. You can see this in theri editor where the word code is in blue color, meaning that it is reserverd word. So again, why you have to use this word?
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: C Structure

Hi,

Thank you very much for your kindly help for so long. I get the solution already. I play around with the code, i put the word before the nxt[] array then it will work finely. I think is the compiler itself declaration pattern.


The program as below:

Code:
#include <reg52.h> 

#define uchar unsigned char 
#define uint unsigned int 

struct { uchar time; uchar dly; } code nxt[] = { {0,10}, {1,20}, {2,30}, {3,40}, {4,50}, {5,60}, {6,70} }; 

void main() 
{ 
uchar i; uchar P1, P2; 

while(1) 
{ 
for(i=0; i<7; i++) 
{ 
P1 = nxt[i].time; 
P2 = nxt[i].dly; 
} 
}

You can try my Keil compiler. While you remove the "code" the data will increase to 14 data because our nxt[] array total got 14 elements but if the "code" is there, so the data size will remain the same size as 11 data according with the whole program. Because of the Microcontroller (data) RAM size is limited so need to shift the data to code.

Thank again for helpping so long.... :D

Thank You.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top