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.

Question about C code - what is the difference?

Status
Not open for further replies.

sunroof

Junior Member level 1
Joined
Jul 6, 2003
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
106
Question about C code

Can someone tell me what is the difference between following 2 definations:

1. char * const buffer

2. char const* const buffer
 

Re: Question about C code

the first one shows the 'buffer' is a constsant, it point to a element of char type
the sencond one shows both the 'buffer' and the context whit char property it pointing to are constant.
 

Re: Question about C code

what if it is:
const char * const buffer
or
const char * const * buffer


sxg said:
the first one shows the 'buffer' is a constsant, it point to a element of char type
the sencond one shows both the 'buffer' and the context whit char property it pointing to are constant.

So, if I have:
buffer = char packet[8];

for 1), I can change the contents of packet, but can not change the location that buffer points to;
for 2), I can not change any element in packet, neither the location that buffer points to.

Am I right? TIA.
 

Question about C code

Suggestions:
1. Enable all of your compiler's warning and error messages. They frequently catch typos and suspicious code.
2. Search for a small command-line utility called "cdecl". It will translate C declarations into semi-english. For example:

cdecl explain const char * const buffer
Declare buffer as const pointer to const char.

cdecl explain const char * const * buffer
Declare buffer as pointer to const pointer to const char.

sunroof said:
So, if I have:
buffer = char packet[8];
You can't have that. It's a syntax error. Maybe you meant this:
char packet[8];
buffer = packet;

If that's true, then:
1. error: assignment of read-only variable `buffer'
2. warning: assignment from incompatible pointer type
 

Re: Question about C code

echo47 said:
Suggestions:
1. Enable all of your compiler's warning and error messages. They frequently catch typos and suspicious code.
2. Search for a small command-line utility called "cdecl". It will translate C declarations into semi-english. For example:

cdecl explain const char * const buffer
Declare buffer as const pointer to const char.

cdecl explain const char * const * buffer
Declare buffer as pointer to const pointer to const char.

sunroof said:
So, if I have:
buffer = char packet[8];
You can't have that. It's a syntax error. Maybe you meant this:
char packet[8];
buffer = packet;

If that's true, then:
1. error: assignment of read-only variable `buffer'
2. warning: assignment from incompatible pointer type
Yes, for that assignment, I did it wrong.
It is:

char const * const buffer;
char packet[8];
......
buffer = packet;



Cool~!
I don't know there is cdecl under linux as such a good helper. :)

But cdecl gives me "parse error" for "char const* const buffer", which I saw a sample code using it.

Any similar tool under Windows? TIA :)
 

Re: Question about C code

Here is an old DOS version of cdecl that runs ok at the Windows command prompt.
It also gives an error message on "char const* const buffer". I've never done it that way. I guess it's legal. Maybe need a newer cdecl.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top